iosobjective-cnsoperationqueuensoperationnsblockoperation

How to know all concurrent NSOperations are completed


My situation is:

What I want to do is:

What I ask is:

Any help is appreciated.

    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    [operationQueue setMaxConcurrentOperationCount:2];

    for (User *user in users) {
        NSBlockOperation *operationObject = [NSBlockOperation blockOperationWithBlock:^{
            [user loadHistoryData:^{
                [NSNotificationCenter postNotificationToMainThread:@"oneUserFetchedHistory"];
            }];
        }];

        [operationQueue addOperation:operationObject];
    }

This question differs from this question because I don't want to chain any requests. I just want to know when they are all finished executing.

This answer has a completion block for only one operation queue.

This answer offers a way to make operation block to wait until async call to loadHistoryData is completed. As writing setSuspended

I could not find an answer for my need. Is there any?


Solution

  • I've used AsyncBlockOperation and NSOperationQueue+CompletionBlock

    Combining them is working for me like this:

      NSOperationQueue *queue = [[NSOperationQueue alloc] init];
      [queue setMaxConcurrentOperationCount:1]; 
      // 1 is serial, more is concurrent
    
      queue.completionBlock = ^{
        [NSNotificationCenter postNotificationToMainThread:@"allUsersFetchedHistory"];
      };
    
      for (User *user in users){
       [queue addOperationWithAsyncBlock:^(AsyncBlockOperation *op) {
         [user loadHistoryData:^{
           [op complete];
           [NSNotificationCenter postNotificationToMainThread:@"oneUserFetchedHistory"];
          }];
        }];
      }