Re: dispatch queues and KVO?
Re: dispatch queues and KVO?
- Subject: Re: dispatch queues and KVO?
- From: Seth Willits <email@hidden>
- Date: Tue, 15 Oct 2013 14:55:38 -0700
On Oct 15, 2013, at 1:43 PM, Todd Heberlein wrote:
> I have an AppDelegate with several ivars that need to be changed by code running out of dispatch queues. Should the setting of these values (because of KVO maybe?) be done by putting a block on dispatch_get_main_queue() and change the ivar there?
They don't need to be unless KVO callbacks are registered and doing things that must be done on the main thread (like update the UI). Assuming that is the case…
> For example, is the following
>
> dispatch_async(dispatch_get_main_queue(), ^{
> self.some_ivar = some_value;
> });
>
> the right design pattern?
…it's "right" only if some other code (for example, the containing code which calls the code above) does not depend on the property to have been updated. If it does, then you may need to do some wackiness like having separate main-thread and non-main-thread values. (A new API in 10.9 does exactly that.)
One problem with ensuring that the property is only updated on the main thread, is that you *must* ensure it's updated on the main thread. If you forget somewhere, something may blow up. So it's debatable whether it's better to ensure that your KVO callback fires off UI/other-main-thread-only-code instead of relying on the the KVO callback to always be called on main thread.
--
Seth Willits
_______________________________________________
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