On May 24, 2005, at 9:31 AM, John Nairn wrote: I have a thread that wants to tell a view to update and then the thread must wait until the view is updated before continuing. This thread worked fine until Tiger. The code fragment is
[myView setNeedsDisplay:YES] NSLog(@"needsDisplay is now %d",(int)[myView needsDisplay]); // it shows "1" [myView displayIfNeeded]; // force update (but was not needed before Tiger) while(YES) { [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.2]]; if([myView needsDisplay]==NO) break; }
But the loop that waits until the view is updated never exits. I checked and drawRect is being called and successfully finished, but needsDisplay is never set back to NO. This was all fine until compiling in Tiger. What has changed in Tiger about updating views in a thread? Or is there another way to have a thread wait until a view has completed an update? ---------------- John Nairn (1-801-581-3413, FAX: 1-801-581-4816)
As Bob Ippolito pointed out, messaging views from a secondary thread is in general not thread-safe and therefore not a recommended practice, on any version of Mac OS X.
If you must force a view to be redrawn synchronously, and block on completion of the redraw, you can have your secondary thread send the view a "display" message on the main thread:
[someControllerObject performSelectorOnMainThread:@selector(displayTheView:) withObject:myView waitUntilDone:YES];
where someControllerObject implements:
- (void)displayTheView:(id)theView { [theView display]; }
Regarding the -needsDisplay behavior you are seeing: Is "myView" fully visible, or is part of it clipped by an ancestor view (e.g. an NSScrollView/NSClipView)? A view may be marked as needing display anywhere within its bounds, including areas that are not within its current "visibleRect" (areas, for example, that are currently scrolled out of view). It is therefore possible for -needsDisplay to report YES after a view's visible area has been redrawn, if occluded portions of the view remain invalidated. On Tiger, this state is respected and preserved more consistently than it was on Panther.
--
Troy Stephens Cocoa Frameworks Apple Computer, Inc.
|