NSOperation and NSOperationQueue look simple. They usually are used to do some background operations. While they are very powerful, actually they can be used to do authentications, play video and show alerts. Furthermore, we can use NSOperation dependency to organize the whole logic in apps. This is an interesting new design pattern that Apple encourages.
Setting NSOperationQueue object maxConcurrentOperationCount = 1 can turn this queue into a serial queue. Reducing the number of concurrent operations does not affect any operations that are currently executing. NSOperationQueueDefaultMaxConcurrentOperationCount is the value of the maximum number of operations based on system conditions.
NSOperation dependency API is very straightforward. You can add or remove dependencies for an operation using the addDependency: or removeDependency: method.
Operation dependency is very useful for complicated operations. Operation dependencies are guaranteed. Based on this, all business logic in apps can be put into NSOperation and these operations are connected by dependencies. Actually WWDC 2015 app was built based on NSOperation dependencies. Each block in the graph below represents an operation.
The below is the example of using NSOperation to open a new view controller, which is from Apple’s advanced NSOperation demo code. One thing needed to notice is that this operation is set not to execute concurrently as this is a synchronous operation. From this example, we can see that the main business logic is put in the operation.