- Introduction to NSOperation and NSOperationQueue
benefits
- GCD based on a higher level of encapsulation, fully object-oriented. But it’s easier to use and more readable than GCD.
- You can add completed blocks of code to execute after the action is complete.
- Add dependencies between operations to easily control the order of execution.
- Set the priority of the operation.
- You can easily cancel an operation.
- Use KVO to observe changes in the execution status of operations: isExecuteing, isFinished, isCancelled.
- use
Use NSOperation subclasses NSInvocationOperation, NSBlockOperation, or custom subclasses to encapsulate operations. Non-thread-safe.
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task) object:nil]; // 2. Call the start method to start the operation [op start]; NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ // todo }]; [op start];Copy the code
- Use custom subclasses derived from NSOperation that operate on the main thread
// xqoperation. h file #import <Foundation/ foundation. h> @interface XQOperation: #import "xqOperation. h" @implementation XQOperation - (void)main {if (! self.isCancelled) { for (int i = 0; i < 2; i++) { [NSThread sleepForTimeInterval:2]; NSLog(@"1---%@", [NSThread currentThread]); XQOperation *op = [[XQOperation alloc] init]; // 2. Call the start method to start the operation [op start];Copy the code
- NSOperationQueue, or directly through
- (void)addOperationWithBlock:(void (^)(void))block;
Operation block. You can set the maximum number of concurrent requests. If it is 1, it is serial; if it is greater than 1, it is parallel.
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; NSInvocationOperation NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task1) object:nil]; NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(task2) object:nil]; NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{// todo}]; [op3 addExecutionBlock:^{ // todo }]; // 3. Run addOperation: add all operations to queue [queue addOperation:op1]; // [op1 start] [queue addOperation:op2]; // [op2 start] [queue addOperation:op3]; // [op3 start]Copy the code
- NSOperation Operation dependency. You can set the dependency as follows
- (void)addDependency:(NSOperation *)op;
- (void)removeDependency:(NSOperation *)op;
@property (readonly, copy) NSArray<NSOperation *> *dependencies;
- NSOperation Common attributes and methods
- (void)cancel; Operations can be cancelled, essentially marking isCancelled status. Method for Checking operation status - (BOOL)isFinished; Check whether the operation is complete. - (BOOL)isCancelled; Determines whether the operation has been marked as cancelled. - (BOOL)isExecuting; Determine whether the operation is running. - (BOOL)isReady; Determines whether an operation is in a ready state. This value depends on the dependency of the operation. Operation synchronization - (void)waitUntilFinished; Blocks the current thread until the operation is complete. Can be used for sequential synchronization of threads. - (void)setCompletionBlock:(void (^)(void))block; CompletionBlock executes the completionBlock when the current operation completes. - (void)addDependency:(NSOperation *)op; Add a dependency that makes the current operation dependent on the completion of the operation op. - (void)removeDependency:(NSOperation *)op; Remove a dependency to remove the dependency of the current operation on op. @property (readonly, copy) NSArray<NSOperation *> *dependencies; Array of all action objects that are executed before the current operation begins.Copy the code
- Common properties and methods of NSOperationQueue
CancelAllOperations - (void)cancelAllOperations; You can cancel all operations on the queue. - (BOOL)isSuspended; Determines whether the queue is paused. YES indicates the pause state, and NO indicates the recovery state. - (void)setSuspended:(BOOL)b; You can set the pause and resume of an operation. YES stands for pause queue, and NO stands for resume queue. Synchronous operation - (void) waitUntilAllOperationsAreFinished; Blocks the current thread until all operations in the queue have completed. Add/get operation '- (void)addOperationWithBlock:(void (^)(void))block; Add an operation object of type NSBlockOperation to the queue. - (void)addOperations:(NSArray *)ops waitUntilFinished:(BOOL)wait; To add an array of operations to the queue, wait whether to block the current thread until all operations are completed - (NSArray *)operations; An array of operations currently in the queue that are automatically cleared when an operation completes. - (NSUInteger)operationCount; Operands in the current queue. Get queue + (id)currentQueue; Gets the current queue, and returns nil if the current thread is not running on NSOperationQueue. + (id)mainQueue; Gets the main queue.Copy the code