views:

30

answers:

1

I aims to release a movieplayer (theMovie) and then start another action (so-called playButtonClicked) after it is completely released. I used performSelector to delay the "playButtonClicked" for 1 second and it works well. The code is:

[theMovie release]; [self performSelector:@selector(playButtonClicked) withObject:nil afterDelay:1];

However, I don't want to always delay 1 second. I want to start the "playButtonClicked" as soon as "theMovie" is completely released. I tried the following code, but it didn't work because [timer userInfo] never is nil. Does anybody know how to check a movieplayer release finished.

[theMovie release];
//[self performSelector:@selector(playButtonClicked) withObject:nil afterDelay:1];

NSTimer *atimer =   [NSTimer scheduledTimerWithTimeInterval:0.1 target:self 
                                                  selector:@selector(waitForReleaseFinish:)
                                                  userInfo: (MPMoviePlayerController*) theMovie repeats:YES];

The code of waitForRleaseFinish: (NSTimer *)timer is:

if ([timer userInfo]==nil)  //here I actually want to test if (theMovie==nil),but I don't know how to do and I'm not sure if it is a correct way to determine the release finished.
{
     [timer invalidate];
    [self playButtonClicked];
}

Look forward to helps. Thank you.

+1  A: 

There is no need.

If you just release the player and then call playButtonClicked, like this:

[theMovie release];
[self playButtonClicked];

It won't execute the second line until the first is completed, or until theMovie is released. This is all on the same thread so it will execute in order. You don't need a timer for this. Although in situations where what you're waiting for to finish executes on a new thread, you would use a callback, rather than guessing how long it takes (which is much less than 1 second!).

Also, just so you don't misunderstand, "completely releasing" is just subtracting the retainCount by one. It will automatically deallocate when it reaches zero.

Just as a side note, why is it important that theMovie is released (deallocated?) before playButtonClicked is executed?

Also, your waitForReleaseFinish: code would work, but it's unnecessary because theMovie would be released before the timer is created.

Mk12
Thank you for your reply. It sounds make sense. Unfortunately, if I do not delay 1-s (even 0.5s), the program doesn't work properly. theMovie release is within a "moviePlayerDidFinsh" and then starts to play another movie. Since in play{}, theMoive is recreated again, it must be released first.MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:[self getMovieURL]]; if (mp) { // save the movie player object self.theMovie = mp; [mp release]; }
vicky
If theMovie is a retaining property, it should release the old value by itself. So if theMovie is a retaining property (and it should be) then you shouldn't manually release it. Just call [self playButtonClicked] and when you set the property to the new movie player it will release the old one.
Mk12