Re: KVO - receptionist design pattern
Re: KVO - receptionist design pattern
- Subject: Re: KVO - receptionist design pattern
- From: Quincey Morris <email@hidden>
- Date: Sun, 01 Dec 2013 10:27:48 -0800
On Dec 1, 2013, at 07:26 , Kevin Meaney <email@hidden> wrote:
> This property is mostly changed on a queue that is not the main queue (gcd queues, not NSOperation queues). Everything works as I would like but I'm concerned that it is only working out of luck rather than proper design.
You didn’t actually state the nature of your concern. You’re worried specifically about the problem of triggering a UI update on a background thread? If so, then, yes, you’ve been lucky if it worked.
> Has something changed in Mavericks so that doing this is no longer a problem, or do I need to fix.
Nothing’s changed in Mavericks AFAIK, so you should fix your code.
> Because this is a simple case (I know when I'm not on the main thread) I can easily fix with just wrapping the modification of the property like so:
>
> __weak AppDelegate *weakSelf = self;
> dispatch_async(dispatch_get_main_queue(), ^
> {
> AppDelegate *strongSelf = weakSelf;
> if (strongSelf)
> strongSelf.isAgentRunning = isRunning ? @"Yes" : @"No";
> });
>
> But of course this is fine in my simple case, and since this is all I need I'll go with this.
Yes, but why are you taking the trouble to “avoid” a reference cycle when none can be formed? What’s wrong with the simple version?
dispatch_async(dispatch_get_main_queue(), ^
{
self.isAgentRunning = isRunning ? @"Yes" : @"No";
});
Certainly the block captures ‘self’ in this case, but nothing other than the queue captures the block.
> But what I suppose I'm asking is, are others using the receptionist design pattern, is it the way to go for a robust solution to this problem?
It’s no robuster than the “simple” solution. It’s useful when the object modifying the property doesn’t or can’t know whether it’s a background thread — for example, when it’s in a 3rd party library you don’t control. But in your own code, you can always use [NSThread isMainThread] to decide whether to update the property synchronously or asynchronously.
_______________________________________________
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