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 protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to