Nsoperation && Nsoperationqueue Use

Source: Internet
Author: User

Nsoperation&&nsoperationqueue using Nsoperation

Used to encapsulate the task we want to execute asynchronously (download image/image filter action), Nsoperation is an abstract class, and NSNumber is the same, it does not have much use in itself and cannot add operations to nsoperationqueue. What's useful is its subclasses.

Nsinvocationoperation

A subclass that inherits from Nsoperation, can encapsulate a method into a task and submit it to the Nsoperationqueue scheduler

Synchronous execution

1 Object:@ "invocation"]; 2 [invocationoperation start];

A nsinvocationoperation object is created here, nsinvocationoperation Adds a method (dosomething:) to the operation, and then we call directly the The Start method, after calling this method, DoSomething: The task in the method is executed synchronously in the current thread

Asynchronous execution

1 Object:@ "invocation"]; 2 3 nsoperationqueue *queue = [[Nsoperationqueue alloc] init]; 4 5 [queue addoperation:invocationoperation];

Also creates a nsinvocationoperation object, nsinvocationoperation Adds a method (dosomething:) to the operation, Doing the task we want to asynchronous in the method, and then handing it over to the queue to dispatch, is executed in the child thread (the object argument is for dosomething: passing parameters)

Nsblockoperation

The same subclass that inherits from Nsoperation, can add multiple blocks to the operation and commit to Nsoperationqueue dispatch

Synchronous execution

1 nsblockoperation *blockoperation = [nsblockoperation blockoperationwithblock:^{23  //  Task a45}]; 6 7 [blockoperation start];   

Creates a nsblockoperation object with a class method and adds a task to the operation, invokes the Start method, and executes synchronously on the current thread

Asynchronous execution

1Nsblockoperation *blockoperation = [Nsblockoperation blockoperationwithblock:^{2 3     //Task A4 5 }];6 7[Blockoperation addexecutionblock:^{8 9     //Task BTen  One }]; A  -[Blockoperation addexecutionblock:^{ -  the     //Task C -  - }]; -  +[Blockoperation start];

After we have created nsblockoperation , we can also add tasks to operation, in which case the system will automatically open a new thread for us to perform all tasks asynchronously

Asynchronous execution submitted to the queue

1 nsblockoperation *blockoperation = [nsblockoperation blockoperationwithblock:^{23  //  Task a45}]; 6 7 Nsoperationqueue *queue = [[Nsoperationqueue alloc] init]; 8 9 [queue addoperation:blockoperation];   

Creates a nsblockoperation object and adds a task, which is then submitted to the queue for asynchronous execution, in which case the system automatically opens the child thread for us and executes the task asynchronously in the child thread

Custom operation

Sometimes the operation that the system gives us does not work well, then we need to make our own definition operation

  1. Create a class (for example: Nsdownloadimageoperation) that inherits from Nsoperation
  2. Overriding the main method in Nsdownloadimageoperation, implementing the task we want in the Main method
  3. Create an auto-free pool yourself in the main method, because if it is an asynchronous operation, you cannot access the auto-free pool in the main thread
  4. It is possible that the operation will be stopped, so we will often use the IsCancelled method in the main method to determine if the operation is stopped and respond accordingly

Other properties

The operation has a Completionblock property that is used to listen to the operation operation after the action is completed.

Nsoperationqueue: operation of Scheduled commits

1. Add operation to the queue

1 nsoperationqueue *queue = [[Nsoperationqueue alloc] init]; 2 3 [queue addoperation:operation];

2. Add a block task directly to the queue

1 nsoperationqueue *queue = [[Nsoperationqueue alloc] init]; 2 3 [queue addoperationwithblock:^{45//  task 67  }];   

3. Maximum number of concurrent queues: At the same time, how many threads can perform operations with the maxconcurrentoperationcount Property Set the maximum number of concurrent

1Nsblockoperation *blockoperation = [Nsblockoperation blockoperationwithblock:^{2 3      for(inti =0; I <3; ++i) {4 5[Nsthread sleepfortimeinterval:.001];6 7NSLog (@"%@", [Nsthread CurrentThread]);8 9     }Ten  One }]; A  -Nsoperationqueue *queue =[[Nsoperationqueue alloc] init]; -  the [Queue addoperation:blockoperation]; -  -[Queue addoperationwithblock:^{ -  +      for(inti =0; I <3; ++i) { -  +[Nsthread sleepfortimeinterval:.001]; A  atNSLog (@"%@", [Nsthread CurrentThread]); -  -     } -  - }]; -  inQueue.maxconcurrentoperationcount =2;


By the above results, we can get the maximum number of concurrent threads in the consent time is 2, although the thread numbers have been opened to 3

4. Cancellation of the queue

Cancel all operation by cancelalloperations method

5. Queue suspend and reply

Suspend Queue (YES), restore (NO) by setsuspended: method

6. Sequencing between operations: Nsoperation can be set to ensure the order of execution

 1 [operationB addDependency:operationA]; // 操作B依赖于操作A, 操作A在操作B之前执行

You can also create a dependency between nsoperation of different queue


Execution order: Operation B, Operation 1, Operation 2, Operation C, Operation 3, Operation D

Operation A with Operation 4 asynchronous execution

Nsoperation && Nsoperationqueue Use

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.