Note that there are some explanatory texts on larger screens.

plurals
  1. PONSOperation completionBlock is called twice
    primarykey
    data
    text
    <p>I'm working on a NSOperation subclass and I came across this very weird issue were the completion block is called twice in a row. The KVO calls seem fine but the completionBlock is still strangely called twice. Am I misunderstanding NSOperation? The documentation says that the completion block is called when <code>isFinished</code> becomes <code>YES</code> and that only happens once in my code:</p> <pre><code>- (void)main { @autoreleasepool { [self willChangeValueForKey:@"isExecuting"]; [self willChangeValueForKey:@"isReady"]; executing = YES; [self didChangeValueForKey:@"isReady"]; [self didChangeValueForKey:@"isExecuting"]; //start the operation } } </code></pre> <p>I then simply set the <code>completionBlock</code> like this:</p> <pre><code>self.completionBlock = ^{ NSLog(@"Completed"); } </code></pre> <p>When it finishes this method is called (it is called just ONCE, i double checked that)</p> <pre><code>- (void)completeOperation { [self willChangeValueForKey:@"isExecuting"]; [self willChangeValueForKey:@"isFinished"]; executing = NO; completed = YES; [self didChangeValueForKey:@"isExecuting"]; [self didChangeValueForKey:@"isFinished"]; } </code></pre> <p>But the completionBlock is called twice and prints "Completed" twice into the console.</p> <p>And here are the methods that indicate the current state:</p> <pre><code>- (BOOL)isReady { if (executing || cancelled || completed) { return NO; } return YES; } - (BOOL)isCancelled { return cancelled; } - (BOOL)isConcurrent { return YES; } - (BOOL)isExecuting { return executing; } - (BOOL)isFinished { return completed; } </code></pre> <p><code>isCancelled</code> never turns to <code>YES</code> in my testing code so that couldn't be the cause of it.</p> <p>I really don't get why the completionBlock is called twice. Even when setting the completion block to nil from inside the completion block it is sometimes called twice which is even stranger.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload