Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C Coroutine

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

like image 759
Christian Stewart Avatar asked Mar 09 '26 07:03

Christian Stewart


1 Answers

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.

like image 165
Chris Hanson Avatar answered Mar 11 '26 20:03

Chris Hanson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!