1. Introduction

NSOperation, NSOperationQueue is based on a higher level of ENCAPSULATION of GCD, is a set of solutions for multi-threading provided by Apple.

NSOperation, the advantages of NSOperationQueue

  1. You can add blocks of code to execute after the operation is complete
  2. Add operation dependencies to control the sequence of operations
  3. Set the priority of the operation
  4. Easy to cancel an operation
  5. Use KVO observe operation state changes (isFinished, isExecuting isCancelled)

2. Procedure

NSOperation needs to work with NSOperationQueue to implement multithreading. Because by default NSOperation alone uses the system to perform operations synchronously, using NSOperationQueue in conjunction with NSOperationQueue makes it easier to perform operations asynchronously.

  1. Create operations (encapsulating an operation into an NSOperation object)
  2. Creating a queue (creating an NSOperationQueue object)
  3. Add the operation to the queue and begin execution

3. Basic use

NSoperation is an abstract class, we can’t use it directly. We need to use its subclasses to encapsulate operations. There are three ways to encapsulate operations:

  1. Using NSInvocationOperation
  2. Using NSBlockOperation
  3. Custom subclass inherited from NSoperation

3.1 Use of NSInvocationOperation

Print result:

Execute in another thread

3.2 Use of NSBlockOperation

NSBlockOperation improves the way addExecutionBlock adds additional operations. These operations can be performed simultaneously (concurrently) in different threads and are considered complete only when all operations have been completed. The blockOperationWithBlock operation may also be executed in other threads (other than the current thread) if many operations are added, depending on the system. You can use addExecutionBlock to add more operations:

3.3 Customizing the use of subclasses inherited from NSoperation

Subclasses of NSoperation are not enough, so we can use custom subclasses to do this. We can define our own operation object by redefining the main method or the start method. Overriding the main method is easier. We don’t need to manage the operation’s property states isFinished and isExecuting. When main isFinished, the operation isFinished.

4. Use of NSOperationQueue

NSOperationQueue contains two types of queues: primary queue and custom queue. Custom queues include both serial and concurrent capabilities. MainQueue: NSOperationQueue *queue = [NSOperationQueue mainQueue];

NSOperationQueue *queue = [[NSOperationQueue alloc]init];

NSOperationQueue controls serial and concurrent

By controlling the maximum number of concurrent maxConcurrentOperationCount implements the serial and concurrent operation

  • The default value is -1, indicating that no control is performed and concurrent execution can be performed
  • If the value is 1, the queue is a serial queue and can only be executed serially
  • If the value is greater than 1, the queue is a concurrent queue and executes concurrently. This value cannot exceed the system limit, even if the system sets infinity to a smaller value than the set value and the default maximum value.

NSOperation Operation dependency

NSOperation controls the execution sequence of operations through operation dependencies. AddDependency – (void)addDependency:(NSOperation *)op; – (void)removeDependency:(NSOperation *)op; @property (readonly, copy) NSArray

*dependencies;

Add dependencies:

NSOperation priority

@property NSOperationQueuePriority queuePriority;

NSOperation and NSOperationQueue are not thread safe. They can be locked and unlocked to achieve thread safety


Note: Suspending or canceling an operation or a queue does not cancel the current operation immediately, but does not proceed until the current operation is complete. The difference between a pause and a cancel is that a suspended action can be resumed and continued, while a canceled action cannot be resumed and continued.