-
NSOperationQueue compared to GCD
- GCD is a more efficient API for C.
- NSOperationQueue can set maximum concurrency, priority, and dependencies to adjust the order of task execution.
- The NSOperationQueue is an object of the OC that can do KVO listening to see if execution is in progress or if it has completed, or if it is cancelled.
- NSOperationQueue can cancel, suspend, and resume operations that have not yet been performed.
- If the COMMUNIST party can solve the problem, we try to use the Communist Party. If GCD cannot solve the problem, we use NSOperationQueue.
-
Simple use of NSOperationQueue
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
//1.
[operationQueue addOperationWithBlock:^{
NSLog(@"curThread = %@",[NSThread currentThread]);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"mainOperationQueue");
}];
}];
//2.
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"curThread = %@",[NSThread currentThread]);
}];
[operationQueue addOperation:blockOperation];
//3.
NSInvocationOperation *invocationOpeartion = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(demo1:) object:@"invocationOperation"];
[operationQueue addOperation:invocationOpeartion];
Copy the code
-
NSOperationQueue differs from the GCD API
-
Can control the maximum number of concurrent requests
- Create asynchronous concurrent queues and asynchronous serial queues
- code
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; // Control the maximum number of concurrent requests, if not written equivalent to asynchronous concurrent queue. If write 1 equals asynchronous serial queue. If write other equivalent to asynchronous concurrent queue operationQueue maxConcurrentOperationCount = 1;for (int i = 0; i < 10; i++) { [operationQueue addOperationWithBlock:^{ NSLog(@"curThread = %@, i = %d",[NSThread currentThread],i); }]; } Copy the code
-
You can add dependencies
- Complex dependencies can be added
- code
operationQueue = [[NSOperationQueue alloc] init]; NSBlockOperation *block1 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"1"); }]; NSBlockOperation *block2 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"2"); }]; NSBlockOperation *block3 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"3"); }]; [block1 addDependency:block3]; [block2 addDependency:block3]; [operationQueue addOperation:block1]; [operationQueue addOperation:block2]; [operationQueue addOperation:block3]; Copy the code
- Set the priority of a task
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; NSBlockOperation *blockOperation1 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"CurThread = %@, task 1",[NSThread currentThread]); }]; NSBlockOperation *blockOperation2 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"CurThread = %@, task 2",[NSThread currentThread]); }]; NSBlockOperation *blockOperation3 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"CurThread = %@, task 3",[NSThread currentThread]); }]; [blockOperation1 setQueuePriority:NSOperationQueuePriorityLow]; [blockOperation1 setQueuePriority:NSOperationQueuePriorityNormal]; [blockOperation1 setQueuePriority:NSOperationQueuePriorityHigh]; [operationQueue addOperation:blockOperation1]; [operationQueue addOperation:blockOperation2]; [operationQueue addOperation:blockOperation3]; Copy the code
- KVO
- (void)viewDidLoad { [super viewDidLoad]; NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; BlockOperationWithBlock :^{sleep(3.0); NSLog(@)"CurThread = %@, task 3",[NSThread currentThread]); }]; [operationQueue addOperation:blockOperation3]; [blockOperation3 addObserver:self forKeyPath:@"finished" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{ if ([keyPath isEqualToString:@"finished"] && object == blockOperation3) { NSLog(@"change = %@",change); }}Copy the code
- Cancel, suspend, resume operations
- (void)viewDidLoad { [super viewDidLoad]; operationQueue = [[NSOperationQueue alloc] init]; operationQueue.maxConcurrentOperationCount = 2; for(int i = 0; i < 100; I++) {[operationQueue addOperationWithBlock:^{sleep(1.0); NSLog(@"i = %d",i); }]; } } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //1. Cancels all operations that have not yet been performed (operationQueue cancelAllOperations) [operationQueue cancelAllOperations]; //2. Suspend and resume tasksif (operationQueue.suspended) { operationQueue.suspended = NO; }else{ operationQueue.suspended = YES; }}Copy the code
-