多线程使用(三)NSOperation的基本使用
1 //创建一个 NSInvocationOperation 对象 2 NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil]; 3 4 //执行操作,没有使用队列默认同步执行 5 [operation2 start];
1 // 使用 NSBlockOperation 封装操作 2 NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ 3 NSLog(@“------%@", [NSThread currentThread]); 4 }]; 5 6 //执行操作 7 [operation start];
1 // NSBlockOperation 再次封装一个操作 2 [operation addExecutionBlock:^{ 3 NSLog(@"---%@", [NSThread currentThread]); 4 }]; 5 6 // NSBlockOperation 再次封装一个操作 7 [operation addExecutionBlock:^{ 8 NSLog(@"---%@", [NSThread currentThread]); 9 }];
1 //封装NSInvocationOperation 2 NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download) object:nil]; 3 NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil]; 4 5 //封装NSBlockOperation 6 NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{ 7 8 NSLog(@"NSBlockOperation------下载图片---%@", [NSThread currentThread]); 9 }]; 10 11 [operation3 addExecutionBlock:^{ 12 NSLog(@"NSBlockOperation------下载图片2---%@", [NSThread currentThread]); 13 }]; 14 15 //创建队列 16 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 17 // 添加操作到队列中,自动执行 18 [queue addOperation:operation1]; 19 [queue addOperation:operation2]; 20 [queue addOperation:operation3]; 21 //使用block方法添加操作 22 [queue addOperationWithBlock:^{ 23 NSLog(@"------操作---%@", [NSThread currentThread]); 24 }];
queue.maxConcurrentOperationCount = 2;
[operation3 addDependency:operation1];
1 NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ 2 //操作 3 }]; 4 operation.completionBlock = ^{ 5 //操作完成后想做事情 6 }; 7 8 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 9 [queue addOperation:operation];
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。