If you want to cancel the timer, you need to turn to cancel the timer, and this means that you must keep the pointer on the timer around, see the response to the request.
Saving a timer reference is the right way to do this, but for completeness, you can also use the -performSelector:withObject:afterDelay: as a poor timer. This call can be canceled using +cancelPreviousPerformRequestsWithTarget: Code example:
- (void) viewDidLoad { [super viewDidLoad]; [self performSelector:@selector(timerTick) withObject:nil afterDelay:10]; }
And then:
- (void) viewWillDisappear { [NSObject cancelPreviousPerformRequestsWithTarget:self]; [super viewWillDisappear]; }
But this is not the right way to do this, because there may be other selection requests awaiting your object that you cancel. Itβs best to keep the timer around so you know exactly what you are canceling.
By the way, its also probably a good idea to start a timer in -viewDidLoad . Download viewing can happen at any time without any relation to viewing.
source share