iOS development-nsoperation and GCD differences

Source: Internet
Author: User
Tags gcd

After Mac OS X 10.6 and iOS4.0, we introduced a schema that makes all threads run more efficiently and makes 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.

Nsoperation is a GCD object-oriented OBJC package, but compared to GCD based on C language development, more efficient, it is recommended that if there is a dependency between the tasks or if you want to listen to the task completion status of the first choice of nsoperation otherwise use GCD

iOS development-nsoperation and GCD differences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.