Re: Threads
Re: Threads
- Subject: Re: Threads
- From: Fritz Anderson <email@hidden>
- Date: Fri, 19 Jul 2013 15:26:51 -0500
On 19 Jul 2013, at 3:09 PM, koko <email@hidden> wrote:
>
> On Jul 19, 2013, at 1:48 PM, Fritz Anderson <email@hidden> wrote:
>
>> Use an NSOperation, or -performSelectorOnMainThread: (you'll have to wrap the call in your own method), or dispatch_[a]sync() to do it on the main thread.
>
> So are you saying that in the following code:
>
> - (void)setNeedsDisplayWrapper:(BOOL)value
> {
> dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
> {
> [self setNeedsDisplay:value];
>
> dispatch_async(dispatch_get_main_queue(), ^
> {
> NSLog(@"completion");
> });
> });
> }
>
>
> All calls made by Cocoa to display the view will be done a separate thread?
That overstates the case. Unless you use the dispatch queue from dispatch_get_main_queue(), you have no guarantee of what thread the block will be run on. Successive calls to dispatch_async() may implicate different threads, or the same thread.
My thought would be (untested, designed in the haste of a busy day, written in mail):
MyClass * __weak weakSelf = self; // I don't know what your self's class is
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf setNeedsDisplay: value];
NSLog(@"setNeedsDisplay: sent");
});
Your code doesn't guarantee that -setNeedsDisplay would be sent on the main thread, which it has to be. The resulting -drawInRect: call to the view would come from the main thread's runloop.
No comment on what you'll have to do to prevent race conditions between two threads altering the state of a view simultaneously. Which I'm pretty sure you're doing. I'd advocate doing only represented-object work (like images) in the background, without reference to the view, then hand the results off to the view (the handoff being completed on the main thread), so the view's methods operate only on the main thread. At handoff, the background thread does _nothing_ further with the data it handed off. nil-out its pointer; consider the handoff to be +1 ownership for the view and -1 for the background.
(And what I just said is probably not adequate. This is concurrency. Nothing is adequate.)
— F
_______________________________________________
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