views:

243

answers:

2

I am first doing add observer call

followed by remove observer in the notification function.

I am certain that removerObserver is called as I see it on the stack However, the app crashes as if a bad memory reference is left over in the notification center.

I think there are 2 possiblities

  1. I am hitting an apple bug

  2. My sequence of invocation is wrong

Here is my code from playvideo function

//Initialize a MPMoviePlayerController object with the movie. moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(movieReadyPlayMovieNow:) name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(moviePlayBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];

Here is Notification called when the movie is done preloading

  • (void) movieReadyPlayMovieNow:(NSNotification*)notification {

@try { if(moviePlayer != nil){
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerContentPreloadDidFinishNotification object:moviePlayer];

[moviePlayer play];

}

} @catch(id exception) { NSLog(@"Error playing."); } }

A: 

You're registering from name:MPMoviePlayerContentPreloadDidFinishNotification notifications from any (nil) object:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieReadyPlayMovieNow:) name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];

But you're de-registering specifically from the moviePlayer object.

[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerContentPreloadDidFinishNotification object:moviePlayer];

Try either registering for the object nil on both, or moviePlayer on both and see if that solves the problem.

amrox
A: 

Removing a notification while running inside a notification may be a Bad Thing.

Try removing the notification in a separate method and invoking that method with performSelector:withObject:afterDelay using a zero delay, or with performSelectorOnMainThread:. Either of these will finish the current run loop and then will execute the removeObserver method outside the notification call.

Steve Weller