Is there any sample code for doing mulitithreading tasks on iPhone?

I have a slow internet task to save and load a file, I would like to do these slow tasks on some background thread. I am wondering if this can be done, and if so, some example code?

Then, upon completion, I want it to pay attention to the main thread so that I can update the interface.

+1


source to share


4 answers


Ultimately, the device your code is running on has a single processor and cannot load large amounts (gigabytes) of data. The best route is probably the one suggested by Ben (NSURLConnection asynchronously), which gives you the added benefit of being able to cleanly cancel and handle error messages. While this is not technically sliced ​​into what you probably think you want, it integrates well with the event loop and is not blocking. If that's not enough yet, I would suggest looking at NSOperation and NSOperationQueue. You can start the NSOperation sub-class object and do the load there (I would still recommend doing it asynchronously there to enable undo, suspend, etc.).



+2


source


Take a look at NSURLConnection. It will load the NSURL (using NSURLRequest) in the background and dispatch the delegate methods regarding its status.



+7


source


Enter the iPhone Dev Center and search for "Introduction to Thread Programming". Or maybe you can login and use this link:

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Multithreading/Introduction/chapter_1_section_1.html#//apple_ref/doc/uid/10000057i-CH1-SW1

+2


source


If you decide that you need a background thread even after using asynchronous HTTP calls to collect data, be sure to wrap the background thread code in a new NSAutoReelasePool and then drop it at the end.

+1


source







All Articles