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 (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