Threadsafe NSOperation implementations

The "Multicore Considerations" section of the NSOperation Class Reference says "When you subclass NSOperation, you must make sure that any overridden methods remain safe to call from multiple threads. If you implement custom methods in your subclass, such as custom data accessors, you must also make sure those methods are thread-safe. Thus, access to any data variables in the operation must be synchronized to prevent potential data corruption."


But I notice that neither the Configuring Operations for Concurrent Execution section of section of the Concurrency Programming Guide nor the WWDC 2015 Advanced NSOperation example contemplate any such synchronization.


Do we have to synchronize our custom accessors for `NSOperation` properties or not?

Interesting. In the Apple example they do this:


- (void)completeOperation {


    [self willChangeValueForKey:@"isFinished"];


    [self willChangeValueForKey:@"isExecuting"];


 


    executing = NO;


    finished = YES;


 


    [self didChangeValueForKey:@"isExecuting"];


    [self didChangeValueForKey:@"isFinished"];


}


In an asynchronous you would usually be off the initial NSOperationQueue that is KVOing isExecuting and isFinished when you generate those KVO notifications (otherwise it wouldn't make much sense to return YES from isAsynchronous).


-(void)start
{
   [self generateExecutingStarted];

    [self doNetworkWorkWithCompletion:^{
      
      [self completeOperation];

     }];

}


Not sure what the key value observing code looks like in NSOperationQueue. I'd use atomic_bool, but every Apple sample I find for an isAsynchronous NSOperation doesn't which looks wrong to me.

Threadsafe NSOperation implementations
 
 
Q