NSOperationQueue串行操作

来源:互联网 发布:佳能mp258清零软件 编辑:程序博客网 时间:2024/05/21 22:51

当使用NSOperationQueue又要求串行操作时,即后一个task对前一个task有依赖关系时,采用如下方式,

方式一:

queue=[[NSOperationQueue alloc] init];

int index=1; 
MyTask *task=[[[MyTask alloc] init] autorelease]; 
task.operationId=index++;

[queue addOperation:task];

task=[[[MyTask alloc] init] autorelease]; 
task.operationId=index++;

if ([[queue operations] count]>0) { 
    MyTask *theBeforeTask=[[queue operations] lastObject]; 
    [task addDependency:theBeforeTask]; 
}

[queue addOperation:task];

方式二:

[queue setMaxConcurrentOperationCount:1];

设置线程池中的线程数,也就是并发操作数。默认情况下是-1,也就是没有限制,同时运行队列中的全部操作。设置为1即为串行,即当前只能执行单个task,以及FIFO原则执行完了之后再执行下一个




原创粉丝点击