Search code examples
iphoneselectordealloc

cancelPreviousPerformRequestsWithTarget does not work in dealloc


-(IBAction)voiceBroadcast
{

    if ([audioPlayer isPlaying]) {
        [audioPlayer stop];
        [NSObject cancelPreviousPerformRequestsWithTarget:self];
    }


    else {
        if (nil != audioPlayer) {
            [audioPlayer play];
            audioPlayer.volume = 0.0;
            [self performSelector:@selector(doBgMusicFadeIn)];
            [self performSelector:@selector(doBgMusicFadeDown) withObject:nil afterDelay:3];
            [self performSelector:@selector(startVoiceBroadcast) withObject:nil afterDelay:4];

        }
    }
}

-(void)dealloc
{
    //position
    [NSObject cancelPreviousPerformRequestsWithTarget:self];

    [super dealloc];

}

The cancelPreviousPerformRequestsWithTarget:self in dealloc is called, but does not work, the selectors still be called. But cancelPreviousPerformRequestsWithTarget:self in voiceBroadcast have no problem. Does the cancelPreviousPerformRequestsWithTarget method could not be used in dealloc?


Solution

  • If you are using UIViewController...

    I don't know why isn't it executed, but you can try putting

    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    

    in -viewDidUnload method, which is called before dealloc.

    More info in UIViewController's reference