

And after it becomes ready, the operation queue will pull it off of the queue and begin executing it. Now, at some point, the operation is going to become ready to execute, and it enters the ready state. So this is the operation when it's initialized and as it's being put onto its operation queue. When you create an NSOperation, it always starts off in a state that we call the pending state. So in order to subclass NSOperation, let's take a look at its lifecycle. The other thing that's really nice about an NSOperation is that since it's a class, you can subclass it and provide your own custom logic on how it executes. NSOperations, on the other hand, can be much longer, for anywhere from a couple of milliseconds to even several minutes, as we will talk about later. Now, in general, NSOperations run for a little bit longer than you would expect a block to run, so blocks usually take a few nanoseconds, maybe at most a millisecond, to execute. Where the queue is a high-level wrapper around a dispatch queue, you can think of an NSOperation as a high-level wrapper around a dispatch block. We don't have to decide this at creation time of our operation queue. The ability to change the behavior of an operation queue like this can be very powerful.

So in this case, our operation queue might be performing two operations at once. So this means that our operation queue can perform multiple operations simultaneously as system resources allow. However, by default, the value of this property is a default value, which means as many as the system allows. The next operation will not begin executing until the previous one has finished. With the max concurrent operation count of 1, the queue will pull off these operations one by one and execute them in order. So let's load up a bunch of operations onto this queue.
WWDC 2015 VIDEO SERIAL
If we set the max concurrent operation count of an NSOperationQueue to be 1, then we essentially make our NSOperationQueue into a serial operation queue. And to understand what this is, let's take a look at a little animation. While you can perform cancellation of dispatch blocks, it is somewhat tricky to do so, but NSOperationQueue makes this quite easy.Īnother thing that you get with NSOperationQueue is a property called the max concurrent operation count. Now, by providing a wrapper around an NSOperationQueue, we can gain some additional functionality.įor example, NSOperationQueue makes it very easy to cancel operations that have not yet begun executing. Hopefully you are all familiar with dispatch queues from using Grand Central Dispatch. And the way to think about an NSOperationQueue is that it's a high-level dispatch queue. So first, let's look at some core concepts.Īny time you use an NSOperation, you will always be using an NSOperationQueue.
WWDC 2015 VIDEO CODE
And finally, we are going to talk about some sample code that we have provided for you. Next, we are going to go beyond those basics and look at the challenges of the WWDC app across and how we solved them. First, we are going to go over the core concepts of NSOperation and how you can understand its API and take advantage of its powerful state machine. Today we are going to be covering three main areas of the WWDC app and NSOperation. So, my name is Dave DeLong, and I am a Frameworks Evangelist at Apple, and I am also the primary engineer on the WWDC app, which hopefully you are all familiar with. And here to take you more in depth of NSOperation and NSOperationQueue and how it actually was used to be able to implement the WWDC app is Dave DeLong. To be able to accomplish numerous different tasks all the way from downloading content from the Internet all the way to synchronizing the database as well as even concepts like presenting alerts or displaying videos.

The WWDC app uses NSOperation and NSOperationQueue extensively. Now, I am pretty sure that all of you have seen this application. These are two extremely powerful classes that can transform your application from running tasks linearly to a hybrid scenario of both the object-orientated and functionally asynchronous concepts. And today we are going to talk about NSOperation and NSOperationQueue. I work in the Frameworks Group, on Foundation. (Applause) PHILIPPE HAUSLER: Good morning.
