On Sep 16, 2015, at 12:56 PM, Charles Srstka <cocoa...@charlessoft.com> wrote:

> - (void)setFoo:(MyObject *)foo {
>       dispatch_async(dispatch_get_main_queue(), ^{
>               [self willChangeValueForKey:@“foo”];
>       });
> 
>       self.privateFoo = foo;
> 
>       dispatch_async(dispatch_get_main_queue(), ^{
>               [self didChangeValueForKey:@“foo”];
>       });
> }

This is not legitimate.  The -willChangeValueForKey: must occur before the 
property, as seen via its getter, has changed.  This is necessary for observers 
who ask for the old value, which can include KVO itself for key paths which go 
through the property.

By dispatching it asynchronously, you allow the setting of privateFoo to 
potentially happen before the -willChangeValueForKey: call.

Also, if multiple background threads set the foo property simultaneously, their 
willChange… and didChange… calls can interleave, which is not kosher.  So, 
using dispatch_sync() wouldn't even solve the problem.

You would have to surround the whole body of the setter with a single dispatch 
call (sync or async).  And the getter would have to also dispatch synchronously 
to the main queue so that it's properly coordinated and consistent with calls 
to the setter.  At that point, it's just better to move the setting of the 
property like Jean did.

Regards,
Ken


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to