-3

Other questions and answers on Stack Overflow speak of observing the operations.count, adding a "Done Operation" etc.

GCD finishedHow do I know all my tasks in Grand Central Dispatch finished?

NSOperationQueue finishes operations

KVO for monitoring all tasks finished

Are there notifications I can subscribe to that send the operation which was finished?

Community
  • 1
  • 1
quantumpotato
  • 9,637
  • 14
  • 70
  • 146
  • 1
    Do you want to know when an operation finishes or when the entire operation queue finishes? – matt May 16 '15 at 21:14
  • Okay, no problem! There are two usual ways, and I've provided both in my answer. – matt May 16 '15 at 22:07

1 Answers1

0

The easiest approach is simply to have your NSOperation's main post an NSNotification at the end of itself. Thus, anyone else can register for that notification and learn that the operation is over (and can come back to the NSOperation to pick up any data stored in properties that may be needed).

Here's a typical structure:

- (void) main {
    if ([self isCancelled])
        return;
    // ... all the action goes here ...
    if (![self isCancelled])
        [[NSNotificationCenter defaultCenter] 
         postNotificationName:@"OperationFinished" object:self];
}

Note that you should make no assumptions about what thread the NSNotification arrives on.

The alternative is to use KVO on the NSOperation's isFinished, but this can get hairy and I'm not sure I would advise it.

matt
  • 515,959
  • 87
  • 875
  • 1,141