标签:
(1)取消队列的所有操作
- (void)cancelAllOperations;
提?:也可以调用NSOperation的- (void)cancel?法取消单个操作
(2)暂停和恢复队列
- (void)setSuspended:(BOOL)b; // YES代表暂停队列,NO代表恢复队列
- (BOOL)isSuspended; //当前状态
(1)设置NSOperation在queue中的优先级,可以改变操作的执?优先级
- (NSOperationQueuePriority)queuePriority;
- (void)setQueuePriority:(NSOperationQueuePriority)p;
(2)优先级的取值
NSOperationQueuePriorityVeryLow = -8L,
NSOperationQueuePriorityLow = -4L,
NSOperationQueuePriorityNormal = 0,
NSOperationQueuePriorityHigh = 4,
NSOperationQueuePriorityVeryHigh = 8
说明:优先级高的任务,调用的几率会更大。
(1)NSOperation之间可以设置依赖来保证执行顺序,?如一定要让操作A执行完后,才能执行操作B,可以像下面这么写
[operationB addDependency:operationA]; // 操作B依赖于操作
(2)可以在不同queue的NSOperation之间创建依赖关系
- (void)viewDidLoad { [super viewDidLoad]; /***********************************************************/ NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test1) object:self]; NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test2) object:nil]; NSBlockOperation *operation3=[NSBlockOperation blockOperationWithBlock:^{ for(int i=0;i<5;i++){ NSLog(@"NSBlockOperation--1--%@",[NSThread currentThread]); } }]; [operation3 addExecutionBlock:^{ for (int i=0; i<5; i++) { NSLog(@"NSBlockOperation--2--%@",[NSThread currentThread]); } }]; //设置操作依赖 //先执行operation2,再执行operation1,最后执行operation3 [operation3 addDependency:operation1]; [operation1 addDependency:operation2]; //不能是相互依赖 // [operation3 addDependency:operation1]; // [operation1 addDependency:operation3]; NSOperationQueue *queue=[[NSOperationQueue alloc]init]; [queue addOperation:operation1]; [queue addOperation:operation2]; [queue addOperation:operation3]; /*****************************监听**************************/ //创建对象,封装操作 NSBlockOperation *operation=[NSBlockOperation blockOperationWithBlock:^{ for (int i=0; i<10; i++) { NSLog(@"-operation-下载图片-%@",[NSThread currentThread]); } }]; //监听操作的执行完毕 operation.completionBlock=^{ //.....下载图片后继续进行的操作 NSLog(@"--接着下载第二张图片--"); }; //创建队列 NSOperationQueue *queue1=[[NSOperationQueue alloc]init]; //把任务添加到队列中(自动执行,自动开线程) [queue1 addOperation:operation]; /***********************************************************/ } -(void)test1{ NSLog(@"NSInvocationOperation-test1-%@",[NSThread currentThread]); } -(void)test2{ NSLog(@"NSInvocationOperation-test2-%@",[NSThread currentThread]); }
标签:
原文地址:http://www.cnblogs.com/sunjianfei/p/5725156.html