Re: Lost memory, GCD, dispatch sources, Cocoa bindings & User interface
Re: Lost memory, GCD, dispatch sources, Cocoa bindings & User interface
- Subject: Re: Lost memory, GCD, dispatch sources, Cocoa bindings & User interface
- From: Ken Thomases <email@hidden>
- Date: Wed, 16 Sep 2015 13:45:11 -0500
On Sep 16, 2015, at 12:56 PM, Charles Srstka <email@hidden> 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 (email@hidden)
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:
This email sent to email@hidden
- Prev by Date:
Re: Lost memory, GCD, dispatch sources, Cocoa bindings & User interface
- Next by Date:
Re: Lost memory, GCD, dispatch sources, Cocoa bindings & User interface
- Previous by thread:
Re: Lost memory, GCD, dispatch sources, Cocoa bindings & User interface
- Next by thread:
Re: Lost memory, GCD, dispatch sources, Cocoa bindings & User interface
- Index(es):