This question already has answers here:
Get notification when NSOperationQueue finishes all tasks

(15个答案)


5年前关闭。




如何获取NSOperationQueue的完成块,这里我想从所有操作的开始到结束旋转 Activity 指示器。
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
// Set the max number of concurrent operations (threads)
[operationQueue setMaxConcurrentOperationCount:3];
[operationQueue addOperations:@[operation, operation1, operation3,...] waitUntilFinished:NO];

谢谢。

最佳答案

您需要实施KVO进行观察。

在操作上查找addDependency,这将帮助您“操作”“完成”,并且所有依赖关系都已解决,它会执行KVN。之后,您可以运行旋转 Activity 指示器的逻辑。您也可以编写一个块。检查以下代码:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSBlockOperation *operationObj = [NSBlockOperation blockOperationWithBlock:^{
    NSLog(@"Show your activity...");
}];

[operationObj setCompletionBlock:^{
    NSLog(@"Operation has finished...");
}];

[queue addOperation: operationObj];

检查以下参考网址

Get notification when NSOperationQueue finishes all tasks

When will completionBlock be called for dependencies in NSOperation

关于ios - 如何获取NSOperationQueue的完成块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27918092/

10-11 14:05