Nsoperation Introduction
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
Subclass of Nsoperation
Nsoperation is an abstract class, does not have the ability to encapsulate operations, it must use its subclasses, there are 3 ways to use the Nsoperation subclass:
- Nsinvocationoperation
- Nsblockoperation
- Customizing subclass Inheritance Nsoperation, implementing the corresponding method internally
Nsinvocationoperation
Steps to use Nsinvocationoperation:
1. Create a Nsinvocationoperation object
Object:(ID) arg;
2. Call the Start method to start the operation
-(void) start;
3. Implement the Sel method in target. Once the operation is performed, the Sel method of the target is called
Note: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 threadoperations are performed asynchronously only if the nsoperation is placed in a nsoperationqueue nsblockoperation To use Nsblockoperation: 1. Create a Nsblockoperation object
+ (ID) Blockoperationwithblock: (void (^) (void)) block;
2. 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
NsoperationqueueNsoperation 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. Adding actions to Nsoperationqueue
-(void) Addoperation: (Nsoperation *) op; -(void) Addoperationwithblock: (void (^) (void)) block;
Maximum number of concurrent
Concurrency is the number of simultaneous tasks, such as 3 threads executing 3 tasks concurrently, and the concurrency number is the associated method for the 3 maximum concurrency number:
- (Nsinteger) maxconcurrentoperationcount; -(void) Setmaxconcurrentoperationcount: (Nsinteger) CNT;
Cancellation, pause, resume of a queue
Cancel all operations on a queue
-(void) cancelalloperations;
Tip: You can also call the Nsoperation-(void) Cancel method to cancel a single operation
pausing and resuming queues
-(void// Yes stands for pause queue, no for recovery queue -(BOOL) issuspended;
Operation Priority Level
n Sets the priority of the nsoperation in the queue, which can change the execution priority of the operation
- (nsoperationqueuepriority) queuepriority; -(void) Setqueuepriority: (nsoperationqueuepriority) p;
The value of the priority level
Nsoperationqueuepriorityverylow =-8L=-4L04 8
Monitoring of operations
Can listen for an operation completed
-(void (^) (void)) Completionblock; -(void) Setcompletionblock: (void (^) (void)) block;
Operation Dependent
Nsoperation can be set up between dependencies to ensure the order of execution, for example, it must be done after operation A to perform operation B, you can write
// Action B depends on action a
You can create a dependency between nsoperation of different queue Custom Nsoperation 1. The steps to customize the nsoperation are simple. Override-(void) The Main method, in which you implement the task you want to perform 2. Override-(void) The Main method's note point itself creates an auto-free pool (because if it is an asynchronous operation that cannot access the main thread's auto-free pool) often passes the-(BOOL) IsCancelled method to detect Whether the operation was canceled and responded to the cancellation cell Download Picture ideas – no sandbox cache cell Download Picture ideas – there is a sandbox cache
iOS multi-thread Nsoperation Learning Summary