Concurrency using NSOperationQueue, NSOperation, and NSInvocationOperation on iPhone OS

Some of the new technologies that most impressed me after attending WWDC 2009 were Grand Central Dispatch (GCD), OpenCL and Blocks. For developers needing to make their applications more performant through concurrency GCD allows developers to look at their application in terms of work units or blocks and how that work might be achieved through queues. It isn’t too far a leap to wonder whether these technologies might be coming to the iPhone in the future. And if they are coming to iPhone OS how can iPhone developers start to take advantage of those technologies now? In June when I had to submit a topic for my talk at 360iDev Denver I thought that this would be good to talk about since operation objects and queues were introduced in Leopard and are available on the iPhone. Using the operation objects NSOperationQueue, NSOperation, and NSInvocationOperation are the easiest way to start learning about Grand Central Dispatch. And using operation objects and queues may allow iPhone applications to take advantage of these technologies and multiple cores when they appear on the iPhone. At the very least it allows concurrency to be achieved in current iPhone applications through a higher level of abstraction than Coca threads. Taking into consideration Apple’s purchase of the chip design firm P.A. Semi in 2008, want ads for OpenCL engineers from Imagination Technologies, the availability of an implementation of blocks for iPhone OS (PLBlocks by Landon Fuller), and ARM’s announcement of the Cortex-A9 dual core chip in mid-September my prediction is that these technologies will appear first in the rumored iTablet device coming in early 2010 followed by a dual core iPhone in mid 2010. Time will tell and only Apple knows.

My presentation Using Concurrency To Improve Responsiveness of iPhone Applications at 360iDev Denver focused on operation objects. During the course of developing the demos for my talk I discovered bugs in the documentation. First, the class reference for NSOperationQueue states that the KVO-compliant properties include operationCount and name. They do not. After trying to determine if it was something I was doing wrong in my application I looked at the header file for NSOperation on iPhone OS 3.0 and 3.1 and on Mac OS X 10.6.1 and it was apparent that it was never implemented for iPhone OS 3.0 and 3.1. It had been discussed at WWDC as an optimization for Snow Leopard but the documentation led me to believe that it was available on the iPhone as well. I filed an enhancement request (radar #7293124) and also provided feedback to correct the documentation. The second issue with the documentation is that it is very easy to become confused when trying to understand Concurrent Versus Non-Concurrent Operations. The bottom line is that nonconcurrent operations (where the KVO property isConcurrent returns NO) execute asynchronously since the operation queue on iPhone OS creates a separate thread on which to run the operation. Concurrent operations (where the KVO property isConcurrent returns YES) on the other hand execute on the current thread because you have determined how you want to handle concurrency in your NSOperation subclass. Note that on Snow Leopard operation queues use GCD and both concurrent and nonconcurrent operations are launched from a separate thread. I’ll upload the demo code that are a part of the presentation in a few days. Have fun with operation objects!

~ by Danton on October 10, 2009.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: