如何向 NSOperation 队列添加多个操作?
我想并行化某些代码,类似于以下示例。我试图将循环的 10 次连续迭代添加到块操作中,我面临的问题是只有第一个块被执行。
NSMutableArray *arrayOfOps=[[NSMutableArray alloc] init];
for (int i=0; i<totalCount()+10; i=i+10) {
NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock:^{
for (int hh=i; (hh<=10)&(hh<totalCount); hh++) {
//some code
}
}];
[arrayOfOps addObject:op];
}
[OperationQueue addOperations:arrayOfOps waitUntilFinished:YES];
[arrayOfOps removeAllObjects];
[arrayOfOps release];
I want to parallelize certain code similar to following sample. I am trying to add 10 successive iterations of a loop to a block operation , the problem I am facing is only the 1st block is getting executed.
NSMutableArray *arrayOfOps=[[NSMutableArray alloc] init];
for (int i=0; i<totalCount()+10; i=i+10) { NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock:^{ for (int hh=i; (hh<=10)&(hh<totalCount); hh++) { //some code } }]; [arrayOfOps addObject:op]; } [OperationQueue addOperations:arrayOfOps waitUntilFinished:YES]; [arrayOfOps removeAllObjects]; [arrayOfOps release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将操作提交到操作队列后,您可以使用“removeAllObjects”从数组中删除操作。操作队列可能只是保留数组,而不是复制它,因此删除操作会影响队列的行为。
只要删除该行就可以了。
You are removing the operations from the array with "removeAllObjects" after submitting it to the operation queue. The operation queue probably just retains the array, rather than copying it, and therefore removing the operations influences how the queue behaves.
Just delete that line and you should be OK.