How to get completion block of NSOperationQueue [duplicate]

家住魔仙堡 提交于 2019-12-11 03:04:06

问题


How to get completion block of NSOperationQueue, here I want to spin activity indicator from start to end of all operation.

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
// Set the max number of concurrent operations (threads)
[operationQueue setMaxConcurrentOperationCount:3];
[operationQueue addOperations:@[operation, operation1, operation3,...] waitUntilFinished:NO];

Thanks.


回答1:


You need to implement KVO to observe.

Go for addDependency on operation which will help you to "isFinished key" of the operation, and all dependency are resolved it performs KVN. After that you can run your logic of spin activity indicator. Also you can write a block as well. Check the following code:

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

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

[queue addOperation: operationObj];

Check following references URL for it

Get notification when NSOperationQueue finishes all tasks

When will completionBlock be called for dependencies in NSOperation



来源:https://stackoverflow.com/questions/27918092/how-to-get-completion-block-of-nsoperationqueue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!