Method called multiple times

I use MPMoviePlayerin my project. I signed up for movie completion notifications and it works well. I am showing an error message when I receive a notification about a video player error. But the problem is that the error warning is displayed several times. This is because more than one notification was received for the same error, and this is also at the same time. I tried using booleans to control the display of alerts, but since notifications were received at the same time, it does not work. Which approach should be applied, please suggest. My code for the notification method:

MPMovieFinishReason reason = [[[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
NSError *errorMsg = [[notification userInfo] valueForKey:@"error"];
NSString *errmsg = [errorMsg localizedDescription];
if (reason == 1 && !errorReceived){
    NSError *errorMsg = [[notification userInfo] valueForKey:@"error"];
    NSString *errmsg = [errorMsg localizedDescription];
    [self showErrorAlert];
}

To register a notification:

NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(moviePlayerDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player];

to remove the observer, in viewWillDisappear

[[NSNotificationCenter defaultCenter]removeObserver:self];
+4
1

,

[[NSNotificationCenter defaultCenter] removeObserver: self name: MPMoviePlayerPlaybackDidFinishNotification object: player];

, .

0

Source: https://habr.com/ru/post/1526875/


All Articles