Re: setNeedsDisplayInRect and performSelectorOnMainThread?
Re: setNeedsDisplayInRect and performSelectorOnMainThread?
- Subject: Re: setNeedsDisplayInRect and performSelectorOnMainThread?
- From: Andrew Farmer <email@hidden>
- Date: Sat, 20 Jan 2007 16:02:02 -0800
On 20 Jan 07, at 15:27, dorian wrote:
I'm doing a multithreaded application which draws based on incoming
network packets (each socket draws to its own view).
The threading docs recommend using performSelectorOnMainThread: for
any
setNeedsDisplay: type of calls that are needed from outside the main
thread, so I am doing that.
This works fine for setNeedsDisplay (v is the view):
[v performSelectorOnMainThread:@selector(setNeedsDisplay:)
withObject:[NSNumber numberWithBool:YES] waitUntilDone:NO];
This only works by accident. -[setNeedsDisplay:] really should be
getting a BOOL, not a NSNumber containing a bool. A non-null pointer
can be interpreted as YES, though, so this happens to work.
However, when I use it for setNeedsDisplayInRect, the display isn't
updated (v is the view, r is a valid rect):
[v performSelectorOnMainThread:@selector(setNeedsDisplayInRect:)
withObject:[NSValue valueWithRect:r] waitUntilDone:NO];
Any idea why this isn't working as expected?
Because an NSValue containing a rect isn't equivalent to a rect.
My first inclination would be to try
[v performSelectorOnMainThread:@selector(setNeedsDisplayInRect:)
withObject:&r waitUntilDone:YES];
and see if that works. Note that I've turned on waitUntilDone, as
performSelectorOnMainThread doesn't grab a copy of the object being
passed as an argument, and letting it continue risks the rect being
destroyed.
It's possible that this'll fail messily if
performSelectorOnMainThread tries to retain the argument - which it
very well might. In that case, you may have to write a helper method
(as an category on NSView) that'll unbox a rect from an NSValue and
use that for setNeedsDisplayInRect.
In general, remember that Cocoa doesn't do any "autoboxing" (like
Java 1.5 does with int and Integer, for example). NSNumber and
NSValue have no "magical" properties; you're only allowed to pass
them when the called routine is specifically expecting them!
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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