Technical Q&A QA1712

Concurrent NSOperations Failing On iOS 4

Q:  I have a concurrent NSOperation that uses NSURLConnection asynchronously. Why doesn't it work on iOS 4?

A: I have a concurrent NSOperation that uses NSURLConnection asynchronously. Why doesn't it work on iOS 4?

In iOS 4 NSOperationQueue was updated to use Grand Central Dispatch (GCD). One consequence of this change is that the -start method of your NSOperation is now always called on a secondary thread. If your -start method schedules run loop callbacks on the current run loop (for example, it creates an NSURLConnection using +[NSURLConnection connectionWithRequest:delegate:]), it's likely that those callbacks will never be called.

This does not represent a change in the semantics of NSOperation. It has always been the case that an NSOperation's -start method must be prepared to run on any thread. However, on iOS 3 the -start method was commonly called on the thread that added the operation to the queue, which meant that bugs like this went unnoticed.

Creating a concurrent, run loop based NSOperation is quite tricky. For a concrete example of how to do this, you should look at the Sample Code 'LinkedImageFetcher'.



Document Revision History


DateNotes
2010-08-23

New document that describes a common problem with concurrent, run loop based, NSOperations (such as those using NSURLConnection) on iOS 4.