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
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
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
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
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 "/>
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