NSOperation, nsoperationqueue
1 NSOperation
With NSOperation and NSOperationQueue, you can also implement multi-threaded programming.
2 NSOperation and NSOperationQueue
- Encapsulate the operation to be executed into an NSOperation object.
- Then add the NSOperation object to the NSOperationQueue.
- The system automatically retrieves NSOperation from NSOperationQueue.
- Put the extracted NSOperation encapsulated operations in a new thread for execution
3 NSOperation subclass
* NSOperation is an abstract class and does not have the ability to encapsulate operations. It must be used as a subclass. * Three NSOperation subclass methods are available: NSInvocationOperation NSBlockOperation, which inherits NSOperation and implements internal methods.
4. Queue type
1> Add the main queue column * [NSOperationQueue mainQueue] * to "main queue column, will be placed in the main thread to execute 2> non-main column * [[NSOperationQueue alloc] init] * Add to "non-main column", will be placed in the sub-thread for execution 2. queue adding task *-(void) addOperation :( NSOperation *) op; *-(void) addOperationWithBlock :( void (^) (void) block;
5 NSInvocationOperation
- Create 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 target's sel method will be called.
Note:
By default, after the start method is called, a new thread is not opened to execute the operation, but the operation is executed synchronously in the current thread.
The operation is performed asynchronously only when NSOperation is put in an NSOperationQueue.
6 NSBlockOperation
- Create an NSBlockOperation object
+ (id)blockOperationWithBlock:(void (^)(void))block;
- Add more operations using addExecutionBlock:
- (void)addExecutionBlock:(void (^)(void))block;
Note: As long as the NSBlockOperation encapsulation operand is greater than 1, the operation will be executed asynchronously.
7 NSOperationQueue
- Functions of NSOperationQueue
NSOperation can call the start method to execute the task, but it is executed synchronously by default.
If you add NSOperation to NSOperationQueue (Operation Queue), the system will automatically perform operations in NSOperation asynchronously.
8 common usage
1> set the maximum number of concurrent jobs
-(NSInteger) maxConcurrentOperationCount;
-(Void) setMaxConcurrentOperationCount :( NSInteger) cnt;
2> Other queue operations
* Cancel all operations
-(Void) cancelAllOperations;
9 dependencies between operations
- Dependencies can be set between NSOperation to ensure the execution sequence.
[OperationB addDependency: operationA]; // operation B depends on Operation A. Operation B is executed only after operation A is completed.
- Note: Do not depend on each other. For example, A depends on B and B depends on.
- You can create dependencies between NSOperation of different queue.
Communication between 10 threads
NSOperationQueue * queue = [[NSOperationQueue alloc] init]; [queue addOperationWithBlock: ^ {// 1. perform some time-consuming operations // 2. return to the main thread [[NSOperationQueue mainQueue] addOperationWithBlock: ^ {}];
11. Returning from other threads to the main thread
1.perform...[self performSelectorOnMainThread:<#(SEL)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>];2.GCDdispatch_async(dispatch_get_main_queue(), ^{});3.NSOperationQueue[[NSOperationQueue mainQueue] addOperationWithBlock:^{}];