How to get completion block of NSOperationQueue [d

2019-07-24 08:11发布

This question already has an answer here:

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条回答
姐就是有狂的资本
2楼-- · 2019-07-24 08:56

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

查看更多
登录 后发表回答