【问题标题】:NSOperation - Group operations and wait until all of them are finished [closed]NSOperation - 分组操作并等待所有操作完成[关闭]
【发布时间】:2018-08-23 09:16:23
【问题描述】:
我有一个 NSOperation 和 NSOperationQueue 对象的子类。我的操作看起来一样,我在 OperationQueue 中添加所有操作,并在完成每个操作后执行完成块。但是有些操作是一个业务组的成员,这个组可能同时在队列中执行。我需要等待,直到每个组完成并在每个组完成后执行完成块。但我不想阻止群组或一个接一个地运行。如何使用 NSOperation 或在其他方法的帮助下制作此变体。
【问题讨论】:
标签:
ios
objective-c
nsoperationqueue
nsoperation
【解决方案1】:
添加一个依赖于每个操作的操作并等待所有操作完成
NSOperationQueue* queue = [NSOperationQueue new];
NSOperation* finalOperation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"ALL IS DONE!");
}];
for (NSUInteger i = 0; i < 10; i++) {
NSOperation* op = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"%zd", i);
}];
[finalOperation addDependency:op];
[queue addOperation:op];
}
[queue addOperation:finalOperation];
Output:
0
2
3
1
5
4
6
7
8
9
ALL IS DONE!