Question about NSTimer and retain

后端 未结 2 1853
故里飘歌
故里飘歌 2020-12-22 10:20

This code works well

@property (nonatomic, retain) NSTimer *timer;
self.timer = [[NSTimer timerWithTimeInterval:kAdsAppearTimeInterval target:self selector:@         


        
相关标签:
2条回答
  • 2020-12-22 10:53

    For a non-repeating timer, if you need a reference to the instance variable, I would not recommend a retain property in its declaration to avoid confusion.

    setting the instance variable (myTimer)

     myTimer = [NSTimer scheduledTimerWithTimeInterval:myTimerInterval
                                                target:self
                                              selector:@selector(myTimerFired:)
                                              userInfo:nil
                                               repeats:NO];
    

    when the timer fires, you can mark the instance variable as nil since its released when the timer is fired

    - (void) myTimerFired: (NSTimer *) theTimer{
    
                myTimer = nil;
            //etc
        }
    

    This way if you have to reference your instance variable (for example to disable the timer when exiting a View controller)

     -(void) onBack {
                 if(myTimer){
                     [myTimer invalidate];
                     myTimer = nil;
                  }
        }
    
    0 讨论(0)
  • 2020-12-22 11:01

    Not a lot to go on... but:

    @property (nonatomic, retain) NSTimer *timer;
    self.timer = [[NSTimer timerWithTimeInterval:kAdsAppearTimeInterval target:self selector:@selector(timerFired:) userInfo:nil repeats:NO] retain];
    

    That'll end up retaining the timer 3 times and self once.

    1. Timer +1 for -retain
    2. Timer +1 for scheduling it
    3. Timer +1 for the property assignment

    4. self +1 for being the target of the timer

    The timer will be released once when fired (because it'll be unscheduled from the run loop). self will be released when the timer is invalidated or released (you shouldn't have to care).

    So, you have two retain counts to account for. The call to retain in the code above is noise; don't bother as the property assignment will retain it.

    That leaves the property's retain. The most obvious way is to release the timer in -dealloc.

    However, unless you need to potentially invalidate the timer before it fires, there is no reason to have an instance variable referring to the timer at all. Even if you do have an iVar, there is no reason to retain the timer either as long as you set self.timer = nil in your timerFired: method (and set it to nil if you invalidate anywhere).

    0 讨论(0)
提交回复
热议问题