Mac OS X 10.6 and iOS4.0 later introduced a schema that would make all threads run more efficiently and make parallel processing applications easier to develop, GCD (Grand Central Dispatch), along with run loops, threads ( including cocoa and POSIX) and operation. GCD has a very lightweight unit of work and concurrency, and the system determines its optimal scheduling. This time there is a problem, nsoperation how to deal with it?
In fact, in our development process through nsoperation and GCD, we find many similarities in the way they are executed, nsoperation and GCD reference comparisons, nsoperationqueue and Dispatch_queue reference comparisons, But there are many differences between the two, the specific difference:
1.The core of GCD is the C language writing system services, execution and operation is simple and efficient, so nsoperation bottom also through GCD implementation, in other words, nsoperation is a higher level of abstraction of GCD, This is the most essential difference between them. Therefore, if you want to customize the task, it is recommended to use nsoperation;
2. Dependencies, Nsoperation can set two nsoperation between the dependencies, the second task depends on the first task to complete execution, GCD cannot set the dependency, but can be achieved through dispatch_barrier_async to achieve this effect;
3.KVO (key value pair observation), nsoperation and easy to determine the current state of operation (whether executed, whether canceled), this gcd can not be judged by KVO;
4. Priority, Nsoperation can set its own priority, but the priority is not necessarily first executed, GCD can only set the priority of the queue, unable to set the priority in the execution of the Block;
5. Inheritance, Nsoperation is an abstract class of practical development commonly used in two classes are nsinvocationoperation and nsblockoperation, as well as we can customize the NSOPERATION,GCD to perform tasks can be freely assembled, There is no inheritance of that high code reuse degree;
6. Efficiency, direct use of GCD efficiency is really more efficient, nsoperation will be a little more overhead, but through the nsoperation can get dependencies, priority, inheritance, key values to observe these advantages, relative to a little bit of overhead is really a cost-effective, the fish and bear paw can not be combined, The choice lies in the developer himself;
On the main differences have been summed up, according to the actual development, GCD use of more, simple and efficient, from becoming in principle, should be the use of high-level abstraction, avoid the use of lower levels of abstraction, then undoubtedly we should choose Nsoperation, Because the complex task can be realized by nsoperation itself, daily or GCD world, after all, GCD has higher concurrency and execution ability.
iOS development-nsoperation and GCD differences