Nstimer Use note and summary
Summarize the following problems encountered in the use of Nstimer:
- 1. Do not stop and release Nstimer in the Dealloc function
If you do this, the object will never be able to call the dealloc function, which will cause a memory leak.
A reasonable explanation is that the Nstimer callback method has a retain property, so the retaincount of the referenced object cannot be dropped to 0 without stopping it, resulting in a dead loop of memory leaks
- 2. Because to achieve similar video software inside, Uiscrollview timed cycle sliding, used in the Nstimer class. Pause in case of a particular event, and restart the timer. Initialization
MyTimer = [nstimer scheduledtimerwithtimeinterval:timeinterval target:self selector: @selector ( Handlemaxshowtimer:) userinfo:nil Repeats:yes];
suspension with [MyTimer invalidate];
Want to restart using [MyTimer fire];
However, run the report memory error when executing [MyTimer fire]. After thinking , add a statement [MyTimer retain] before [MyTimer fire], no exception after execution. Personal guessing when performing initialization
MyTimer = [Nstimer scheduledtimerwithtimeinterval:timeinterval target:self selector: @selector ( Handlemaxshowtimer:) userinfo:nil Repeats:yes];
[mytimer invalidate" should not be suspended, but the cancellation of the timer, there is release operation. So it crashes after I execute [mytimer fire.
So every time I try to start the timer again, I'm going to initialize it again.
iOS development--Practical technology OC Chapter &nstimer use points of attention and summary