Using Nsoperation and Nsoperationqueue-Shangming

Source: Internet
Author: User

Using Nsoperation and Nsoperationqueue

Nsoperation vs. Grand Central Dispatch (GCD)

Before Mac OS X v10.6 and IOS4, Nsoperation and Nsoperationqueue were different from GCD, and they used a completely different mechanism.

Starting with Mac OS X v10.6 and IOS4, Nsoperation and Nsoperationqueue are built on GCD. As a good case, Apple recommends using the highest level of abstraction, but when the assessment shows that there is a need, it suddenly drops to a lower level.

Here's a quick comparison of the two, which will help you decide when and where to use GCD or nsoperation and nsoperationqueue;

    • GCD is a lightweight way to represent the units of a task that will be executed concurrently. You don't need to plan these units; The system will plan for you. Adding a dependency to a block is a daunting task. Canceling or pausing a block will create extra work for a developer! :]
    • Nsoperation and Nsoperationqueue compare gcd with a little extra overhead, but you can add attachments in multiple operations (operation). You can reuse operations, cancel them, or pause them. Nsoperation and Key-value observation (KVO) are compatible; For example, you can listen to Nsnotificationcenter to get an operation to start executing.

Nsoperation API

The Nsoperation class has a fairly brief statement. To customize an action, you can follow these steps:

    1. Inheriting the Nsoperation class
    2. Overriding the "main" method
    3. Create a "Autoreleasepool" in the "main" method
    4. Put your code in "Autoreleasepool" (Arc also needs to be placed in Autoreleasepool, this is to be compatible with MRC code)

For you to be able to test the code, create a new class that inherits from Nsoperation and is written in the following form:

Before you proceed:

Note that this task will only be executed if a task is added to the queue.

The following code is a concurrent execution of 2 tasks, and the maximum number of concurrent queues is 4:

The priority that the task has:

When you add an action to a queue, nsoperationqueue browses all operations before invoking the "Start" method on the operation. Operations that have higher priority are executed first. Operations with the same priority are executed in the order in which they are added to the queue (FIFO).
(Historical note: In 1997, the embedded system in the Mars rover encountered a priority reversal problem, perhaps the most expensive example of a proper handling of priorities and mutexes.) Want to know more about the background of this event, can see this website: http://research.microsoft.com/en-us/um/people/mbj/Mars_Pathfinder/Mars_Pathfinder.html)

Please add the following delay method:

Start the task first, and cancel the task after 1s clock:

There is a callback at the end of each task to indicate the end of the task:

dependencies between tasks:

With Down point summary:

  • Check the IsCancelled property frequently. If the operation does not need to be executed, you do not want to run it in the background! (After you set the Cancell from the outside, you need to implement the Cancell in the method, oh, note, pro)
  • You do not need to rewrite the "Start" method. However, if you decide to rewrite the "Start" method, you have to deal with attributes such as isexecuting, isfinished, Isconcurrent, and IsReady. Otherwise, your operation class will not work correctly.
  • Once you've added an action to a queue (an instance of Nsoperationqueue), you're responsible for releasing it (if you don't use arc). Nsoperationqueue takes ownership of the action object, invokes the "Start" method, and then ends up responsible for releasing it.
  • You cannot reuse an action object. Once it is added to a queue, you lose ownership of it. If you want to use the same action class again, you must create a new instance variable.
  • An end operation cannot be restarted.
  • If you cancel an operation, it will not happen immediately. It will be canceled at some point in the future when someone explicitly checks iscancelled = = YES in the "main" function, otherwise the operation will continue until it is complete. See
  • If an operation completes successfully, fails, or is canceled, the value of isfinished is always set to Yes. So do not think isfinished = = yes means that all things have been done smoothly-in particular, if you have from the code inside the attributes (dependencies), you should pay more attention!

Nsoperationqueue API

To view all tasks for the current concurrency operation:

Suspend and resume operations:

This method suspends or resumes the execution of operations. Suspending a queue prevents that queue from starting additional operations. In other words, operations that is in the queue (or added to the queue later) and is not yet executing is prevented fro M starting until the queue is resumed. Suspending A queue does not stop operations that is already running.

This method suspends or resumes a task that executes. Suspending a queue can block tasks that do not start in the queue. In other words, a task that has not yet started in the task queue is suspended until the pending operation is resumed. Suspending a queue does not stop an already executing task Oh, pro.

Cancel all actions:

To cancel all operations in a queue, you simply call the "cancelalloperations" method. Remember reminding you to check the iscancelled attribute frequently in nsoperation?
The reason is that "cancelalloperations" doesn't do much work, he just calls the "Cancel" method on every operation in the queue-it doesn't make much of a difference! :] If an action does not start and then you call the "Cancel" method on it, the operation is canceled and removed from the queue. However, if an operation is already executing, it is up to the individual operation to identify the revocation (by checking the IsCancelled property) and then stop the work it does.

A simple block-form Queue task:

The above is basically finished how to use Nsoperation and Nsoperationqueue, Pro.

Using Nsoperation and Nsoperationqueue-Shangming

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.