Use of iOS multi-thread nsoperation

Source: Internet
Author: User
Tags gcd

    • Steps for using Nsoperation objects

The Nsoperation object describes an operation task, and the Nsoperationqueue object describes a task queue, which is equivalent to GCD dispatch_queue_t

Nsoperation and Nsoperationqueue can be task is object-oriented encapsulation above GCD

GCD provides a lower level of control, and Nsoperationqueue implements some handy features on top of GCD, which are often the best and safest choice for developers

Basic Use steps:

1) define the operation queue

2) Define the operation

3) Adding actions to the queue

Tip: Once the operation is added to the queue, the operation is immediately scheduled for execution


    • Task Queue Object

Nsoperationqueue describes the task queue, using two types of queues:

1) The home row, where the task is executed only in the main thread, as in the GCD master queue

+ (Nsoperationqueue *) mainqueue

2) Custom queue, where tasks are executed in child threads, as with GCD global queues

Create using the Alloc+init method

Other ways to Nsoperationqueue:

-(void) cancelalloperations//Cancel All Tasks-(void) waituntilalloperationsarefinished//wait until all tasks end @property Nsinteger Maxconcurrentoperationcount//Queue maximum number of concurrent tasks @property (assign) dispatch_queue_t Underlyingqueue//gcd the underlying queue


    • Task object and its subclasses

Nsoperation describes a task that is an abstract type and should use its subclasses

Nsblockoperation and Nsinvocationoperation


1) Nsblockoperation uses block to describe a task:

+ (Instancetype) Blockoperationwithblock: (void (^) (void)) block

You can also add a block to describe multiple tasks

-(void) Addexecutionblock: (void (^) (void)) block

2) Nsinvocationoperation How to use a task:

-(Instancetype) Initwithtarget: (ID) Target selector: (SEL) Sel object: (ID) arg

The specified method can have a return value, using the result property to get

@property (ReadOnly, retain) ID result


    • Add a task to the Task level column

To use the Addoperation method of a task queue object:

-(void) Addoperation: (nsoperation *) operation//does not block execution of the current thread-(void) Addoperations: (nsarray<nsoperation *> *) Ops Waituntilfinished: (BOOL) wait//can block the current thread

There is also a way to add tasks that directly pass the BLOCK:

-(void) Addoperationwithblock: (void (^) (void)) block


Summary: Three ways to add tasks:

1) Addoperationwithblock:

2) Add Nsblockoperation task

3) Add Nsinvocationoperation task


    • Download Images asynchronously

Task 1:bo1 object that describes a download task

Task 2:bo2 object that displays the downloaded image object

Task 1 is added to the custom task queue, and the child thread executes the

Task 2 is added to the primary queue and executed on the main thread

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7C/AF/wKioL1bWVYnQ5GygAAH6QDPot1U541.png "title=" screen shot 2016-03-02 a.m. 10.51.08.png "alt=" Wkiol1bwvynq5gygaah6qdpot1u541.png "/>



    • Dependency of the task

task dependencies make it easy to control the sequencing of multiple task executions

Add dependencies using Nsoperation's adddependency: Method

-(void) Adddependency: (nsoperation *) operation

Such as: Modify the above code

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7C/AF/wKioL1bWVdGzrP6XAAHq-XEo1bk618.png "title=" screen shot 2016-03-02 a.m. 10.52.21.png "alt=" Wkiol1bwvdgzrp6xaahq-xeo1bk618.png "/>

Where Self.img is a newly added UIImage property


This article is from the "Teacheran" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1746634

Use of iOS multi-thread nsoperation

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.