What is difference between self.timer = nil vs [self.timer invalidate] in iOS?

前端 未结 3 1607
故里飘歌
故里飘歌 2020-12-10 14:48

Can anyone explain me self.timer=nil vs [self.timer invalidate]?

What exactly happens at the memory location of self.timer?

3条回答
  •  误落风尘
    2020-12-10 15:03

    Once you have no need to run timer, invalidate timer object, after that no need to nullify its reference.

    This is what Apple documentation says: NSTimer

    Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes the timer (and the strong reference it had to the timer), either just before the invalidate method returns or at some later point. Once invalidated, timer objects cannot be reused.

提交回复
热议问题