views:

38

answers:

2

Objective-C - how might I be able to create a objective-c coroutine? When I have a IBAction called by a button it freezes the app until the IBAction finishes retreiving data (from a web site). How might I have this run as a coroutine to the main app?

Thanks, Christian Stewart

+1  A: 

A coroutine is a specific implementation technique. What you want to do is do work in the background without blocking your user interface.

There are two primary techniques for doing this on iOS and Mac OS X: Run loops and threads.

If you are just downloading some data via an HTTP URL, you can start the download and tell it to notify you when done or when it has some data for you. The URL download will manage its own concurrency using the current run loop, and your application will be able to interact with the user.

If you need to perform your own processing, you can spin off a background thread to do it using NSOperationQueue and NSOperatipn, using GCD (dispatch_*), or using NSThread directly. If you do this you need to understand concurrent programming, protecting shared state using mutexes (locks) and other synchronization mechanisms, and so on.

For just a URL download, use NSURLDownload's own concurrency support. For more processing-heavy work, give NSOperation a try.

Chris Hanson
A: 

Have a look at URL Loading System Programming Guide.

You are probably interested in NSURLConnection, you setup the URL and provide delegates. Your delegates get called with data and when the connection/download is finished.

stefanB