Nsoperation and Nsoperationqueue are more powerful abstractions done on a gcd basis, and because of the more gcd use, there are relatively few introductions to them.
1.NSOperation Basic Concepts
The role of 1.NSOperation
- Multithreaded programming is also possible with nsoperation and Nsoperationqueue
2.NSOperation and Nsoperationqueue specific steps to implement multithreading
- Encapsulates the action that needs to be performed into a Nsoperation object
- Then add the Nsoperation object to the Nsoperationqueue
- The system will automatically remove the nsoperation from the Nsoperationqueue
- Put the removed Nsoperation encapsulated operation into a new thread to execute
2. Use 1 specifically. Nsinvocationoperation
Create a Nsinvocationoperation object
- (id)initWithTarget:(id)target selector:(SEL)sel object:(id)arg;
Call the Start method to start the operation
- (void)start;
Once the operation is performed, the Sel method of the target is called
- (void) invocation{//Note: The parent class does not have the ability to encapsulate operations //1. Encapsulating TasksNsinvocationoperation *OP1 = [[Nsinvocationoperation alloc] Initwithtarget: SelfSelector@selector(Run) object:Nil];//2. To perform a task, you must call start[OP1 start]; Nsinvocationoperation *OP2 = [[Nsinvocationoperation alloc] Initwithtarget: SelfSelector@selector(run2) object:Nil]; [OP2 start];} - (void) run{NSLog(@"%@", [NsthreadCurrentThread]);} - (void) run2{NSLog(@"%@", [NsthreadCurrentThread]);}
Attention:
- By default, when the Start method is called, it does not open a new thread to perform the operation, but instead performs the operation synchronously on the current thread
- Operations are performed asynchronously only if the nsoperation is placed in a nsoperationqueue
2.NSBlockOperation
Create a Nsblockoperation object
+ (id)blockOperationWithBlock:(void (^)(void))block;
Add more actions by Addexecutionblock: method
- (void)addExecutionBlock:(void (^)(void))block;
Note: The operation is performed asynchronously as long as the nsblockoperation encapsulated operand > 1
Code Implementation:-(void) blockoperation{//1. Encapsulating TasksNsblockoperation *OP1 = [Nsblockoperation blockoperationwithblock:^{//Main thread NSLog(@"1---%@", [NsthreadCurrentThread]); }];//2. Additional Tasks //Note: In the absence of a queue, if additional tasks are added to Blockoperation, other tasks are executed in the child thread[Op1 addexecutionblock:^{NSLog(@"2---%@", [NsthreadCurrentThread]); }]; [Op1 addexecutionblock:^{NSLog(@"3---%@", [NsthreadCurrentThread]); }];//3. Start a task[OP1 start];}
3. Custom Nsoperation
1. The steps to customize Nsoperation are simple
- Rewrite-(void) The Main method, where you implement the task you want to perform
2. Override-(void) The note point of the Main method
- Create an auto-free pool yourself (because if it is an asynchronous operation, you cannot access the main thread's auto-free pool)
- Often pass-(BOOL) IsCancelled method detects if the operation is canceled and responds to cancellation
3.NSOperationqueue
The role of 1.NSOperationQueue
- Nsoperation can invoke the Start method to perform a task, but it is performed synchronously by default
- If you add nsoperation to the Nsoperationqueue (action queue), the system automatically executes the operations in Nsoperation asynchronously
2. Add action to Nsoperationqueue
- (void)addOperation:(NSOperation *)op;- (void)addOperationWithBlock:(void (^)(void))block;
Code implementation:
- (void) blockqueue{//1. Create a queueNsoperationqueue *queue = [[Nsoperationqueue alloc] init];//2. Create a taskNsblockoperation *OP1 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"1 = =%@", [NsthreadCurrentThread]); }]; Nsblockoperation *OP2 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"2 = =%@", [NsthreadCurrentThread]); }];//NOTE: If you are using block to encapsulate a task, there is a much easier way //As long as the Addoperationwithblock: method is invoked using the queue, the system is automatically encapsulated into a nsblockoperation \And then add it to the queue. [Queue addoperationwithblock:^{NSLog(@"3 = =%@", [NsthreadCurrentThread]); }];//3. Add Task to queue[Queue ADDOPERATION:OP1]; [Queue addoperation:op2];} - (void) invocationqueue{//1. Create a queue / * What queues are in GCD: Concurrency: Create yourself, Global serial: Create yourself, Home row nsoperationqueue: Main queue: Mainqueue Create yourself: will execute in child threads * /Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];//2. Create a task //As long as the queue is created by itself, it executes in the child thread //And the default is concurrent executionNsinvocationoperation *OP1 = [[Nsinvocationoperation alloc] Initwithtarget: SelfSelector@selector(Download1) object:Nil]; Nsinvocationoperation *OP2 = [[Nsinvocationoperation alloc] Initwithtarget: SelfSelector@selector(download2) object:Nil];//3. Adding tasks to the queue //As soon as the task is added to the queue, the queue automatically calls start[Queue ADDOPERATION:OP1]; [Queue addoperation:op2];} - (void) download1{NSLog(@"1 = =%@", [NsthreadCurrentThread]);} - (void) download2{NSLog(@"2 = =%@", [NsthreadCurrentThread]);}
3. Maximum number of concurrent
1. What is the concurrency number:
- Number of concurrently executed tasks
- For example, open 3 threads at the same time to perform 3 tasks, the concurrency number is 3
2. Correlation method of maximum concurrency number:
- (NSInteger)maxConcurrentOperationCount;- (void)setMaxConcurrentOperationCount:(NSInteger)cnt;
3. Cancellation, suspension, recovery of the queue
1. Cancel all operations on a queue
- (void)cancelAllOperations;
Tip: You can also call the Nsoperation-(void) Cancel method to cancel a single operation
2. Pausing and resuming queues
- (void)setSuspended:(BOOL)b; // YES代表暂停队列,NO代表恢复队列- (BOOL)isSuspended;
4.NSOperation Other usage
1. Operational dependencies
A dependency can be set between 1.NSOperation to guarantee the order of execution
For example, you must let operation a finish, before you can perform operation B, so write
[operationB addDependency:operationA]; // 操作B依赖于操作A
2. You can create a dependency between nsoperation of different queue
3. Note: Do not rely on each other:
- For example a relies on b,b dependency a
4. Monitoring of the operation
Can listen for an operation completed
- (void (^)(void))completionBlock;- (void)setCompletionBlock:(void (^)(void))block;
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Multithreading---nsoperation and nsoperationqueue