iOS Multi-threaded series (ii)------nsoperation and Nsoperationqueue

Source: Internet
Author: User

Nsoperation is an abstract class, it can be used directly using the system provides two sub-classes: Nsinvocationoperation, nsblockoperation, you can also write a class, inherit nsoperation.

There are 2 forms of nsoperation: Concurrent, non-concurrent

(1) Concurrent execution

Concurrent execution you need to overload the following 4 methods

-(void) Start//Execute task main function, thread run entry function

-(BOOL) isconcurrent//whether to allow concurrency, return yes, allow concurrency, return no not allowed. Default returns NO

-(BOOL) isexecuting

-(BOOL) isfinished//Whether has been completed, this must be overloaded, or put in the nsoperationqueue in the nsopertaion can not be released normally

For example, Testnsoperation:nsoperaion overloads the above 4 methods, declares a nsoperationqueue,

Nsoperationqueue *queue = [[[[Nsoperationqueue alloc] init] autorelease];

[Queue addoperation:testnsoperation];

Once an operation is queued, the queue starts and processes it (that is, the main method that invokes the action Class). Once the operation completes the queue, it is freed. In this example, it automatically calls the start function in Testnsoperation.

If you need more than one nsoperation, you need to set some properties of the queue, and if there are dependencies between multiple nsoperation, you can also set it up, refer to the API documentation.

You can set up the number of operations that a colleague runs on the operations queue: [Queue setmaxconcurrentoperationcount:2];

iOS Multi-threaded series (ii)------nsoperation and Nsoperationqueue

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.