An issue with non-blocking NSAnimations
An issue with non-blocking NSAnimations
- Subject: An issue with non-blocking NSAnimations
- From: Oleg Krupnov <email@hidden>
- Date: Mon, 28 Jun 2010 16:32:56 +0300
Hi,
I am animating a custom NSView using an NSAnimation. Depending on the
current progress value, the view draws a different thing in its
drawRect:
In the animation's setCurrentProgress: routine, I call [view
setNeedsDisplay:YES] to invalidate the view and cause it repaint.
However, in this way the view is not updated immediately, which is
required to create the animation effect. So I also call [view
displayIfNeeded]
So the code looks like this:
// in NSAnimation subclass
- (void)setCurrentProgress:(NSAnimationProgress)progress
{
[super setCurrentProgress:progress];
[view setNeedsDisplay:YES];
[view displayIfNeeded];
}
This code worked, but today I have discovered a great trouble with it.
Namely, when the animation works in non-blocking mode, this code is
actually executed in a separate thread. Therefore, if the main thread
is doing some data refreshing in the meantime, I get crashes and
zombies. So I corrected the code as follows:
- (void)setCurrentProgress:(NSAnimationProgress)progress
{
[super setCurrentProgress:progress];
[view setNeedsDisplay:YES];
if ([NSThread isMainThread])
{
[view displayIfNeeded];
}
else
{
[view performSelectorOnMainThread:@selector(displayIfNeeded)
withObject:nil waitUntilDone:NO];
}
}
This code now seems to work okay, but I am looking for an advice if
I've got it right. I believe all drawing should occur in the main
thread, and so I wonder what is the best practice of forcing the
custom view to update in the course of a non-blocking NSAnimation.
Thanks!
Oleg.
_______________________________________________
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