<span id="Label3"></p><p><p>Http://www.cocoachina.com/ios/20150710/12444.html</p></p><p><p>http://blog.csdn.net/meegomeego/article/details/22270745</p></p><p><p></p></p><p><p>When the former was sorting out the Company's project, it found that the old code had a memory leak when using Nstimer. And then sorted out some of the Nstimer related Content. Relatively simple, you laughed at.</p></p><p><p><strong>Nstimer</strong></p></p><p><p><strong>Kindle</strong></p></p><p><p>Let's use Nstimer to make a simple timer that outputs fire at the console every 5 seconds. This is the way to take it for granted:</p></p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">12345678910111213141516</td> <td class="code"><code class="js plain"><code class="js plain">@interface DetailViewController ()</code></code><code class="js plain"><code class="js plain">@property (nonatomic, weak) NSTimer *timer;</code></code><code class="js plain"><code class="js plain">@end</code></code><code class="js plain"><code class="js plain">@implementation DetailViewController</code></code><code class="js plain"><code class="js plain">- (IBAction)fireButtonPressed:(id)sender {</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">_timer = [NSTimer scheduledTimerWithTimeInterval:3.0f</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">target:self</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">selector:@selector(timerFire:)</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">userInfo:nil</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">repeats:YES];</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">[_timer fire];</code></code><code class="js plain"><code class="js plain">}</code></code><code class="js plain"><code class="js plain">-(void)timerFire:(id)userinfo {</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">NSLog(@</code></code><code class="js string"><code class="js string">"Fire"</code></code><code class="js plain"><code class="js plain">);</code></code><code class="js plain"><code class="js plain">}</code></code><code class="js plain"><code class="js plain">@end</code></code></td> </tr> </tbody> </table><p><p>It does output fire every 3 seconds after it runs, but when we jump from this interface to other interfaces, we find that the console is still continuously outputting fire. It seems that the Timer has not stopped.</p></p><p><p><strong>Invalidate</strong></p></p><p><p>Since there is no stopping, we add invalidate method to the dealloc of Demoviewcontroller:</p></p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1234</td> <td class="code"><code class="js plain">-(void)dealloc {</code><code class="js spaces"> </code><code class="js plain">[_timer invalidate];</code><code class="js spaces"> </code><code class="js plain">NSLog(@</code><code class="js string">"%@ dealloc"</code><code class="js plain">, NSStringFromClass([self class]));</code><code class="js plain">}</code></td> </tr> </tbody> </table><p><p>Run again, or there is no stop. The reason is that when the Timer is added to runloop, it is runloop strongly referenced:</p></p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="js plain">Note </code><code class="js keyword">in</code><code class="js plain">particular that run loops maintain strong references to their timers, so you don’t have to maintain your own strong reference to a timer after you have added it to a run loop.</code></td> </tr> </tbody> </table><p><p>Then the Timer will have a strong reference to Target (that is, self):</p></p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1</td> <td class="code"><code class="js plain">Target is the object to which to send the message specified by aSelector when the timer fires. The timer maintains a strong reference to target until it (the timer) is invalidated.</code></td> </tr> </tbody> </table><p><p>In other words, Nstimer strongly quoted self, which led to self being unable to be released, so he could not go to Self's dealloc.</p></p><p><p>That being the case, we can add another invalidate button:</p></p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">123</td> <td class="code"><code class="js plain">- (IBAction)invalidateButtonPressed:(id)sender {</code><code class="js spaces"> </code><code class="js plain">[_timer invalidate];</code><code class="js plain">}</code></td> </tr> </tbody> </table><p><p>well, That's Ok. (it was said in the SOF that after invalidate the execution of _timer = nil, failed to understand why, if you know the reason can tell me:)</p></p><p><p>In the documentation for the Invalidate method there is this passage:</p></p><p><p>You must send this message from the thread on which the timer is Installed. If you send the "this message" from another thread, the input source associated with the timer is not a removed from its run L oop, which could prevent the thread from exiting Properly.</p></p><p><p>Nstimer which thread is created on which thread to stop, otherwise it will cause the resource to not be released Correctly. It looks like a lot of pits.</p></p><p><p><strong>Dealloc</strong></p></p><p><p>So the question is: if I just want this nstimer to be output until Demoviewcontroller is destroyed, how do I stop it?</p></p> <ul class=" list-paddingleft-2"> <ul class=" list-paddingleft-2"> <li><p>Nstimer is Runloop strong reference, if you want to release call invalidate Method.</p></li> <li><p>But I want to invoke the Invalidate method in Demoviewcontroller's dealloc, but self is strongly referenced by Nstimer.</p></li> <li><p>So I'm going to release Nstimer first, but I can't release it without calling the invalidate Method.</p></li> <li><p>However you do not enter into the Dealloc method I cannot call the invalidate Method.</p></li> <li><p>Well...</p></li> </ul> </ul><p><p><strong>Hwweaktimer</strong></p></p><p><p><strong>Weakself</strong></p></p><p><p>The crux of the problem is that self is nstimer strongly quoted, and if we can break the strong citation problem, it will be solved naturally. So a very simple idea is: weakself:</p></p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">123456</td> <td class="code"><code class="js plain"><code class="js plain">__weak </code></code><code class="js keyword"><code class="js keyword">typeof</code></code><code class="js plain"><code class="js plain">(self) weakSelf = self;</code></code><code class="js plain"><code class="js plain">_timer = [NSTimer scheduledTimerWithTimeInterval:3.0f</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">target:weakSelf</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">selector:@selector(timerFire:)</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">userInfo:nil</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">repeats:YES];</code></code></td> </tr> </tbody> </table><p><p>There is no egg, however, and the only difference between __weak and __strong here is that if self is freed during the execution of these two lines of code, the target of Nstimer will become Nil.</p></p><p><p><strong>Target</strong></p></p><p><p>Since there is no way to pull self out through __weak, we can make a false target to Nstimer. This fake target is similar to a middle agent, and the only job it has to do is to step forward and pick up the Nstimer's strong references. The class declaration is as Follows:</p></p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">1234567891011121314</td> <td class="code"><code class="js plain"><code class="js plain">@interface HWWeakTimerTarget : NSObject</code></code><code class="js plain"><code class="js plain">@property (nonatomic, weak) id target;</code></code><code class="js plain"><code class="js plain">@property (nonatomic, assign) SEL selector;</code></code><code class="js plain"><code class="js plain">@property (nonatomic, weak) NSTimer* timer;</code></code><code class="js plain"><code class="js plain">@end</code></code><code class="js plain"><code class="js plain">@implementation HWWeakTimerTarget</code></code><code class="js plain"><code class="js plain">- (void) fire:(NSTimer *)timer {</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js keyword"><code class="js keyword">if</code></code><code class="js plain"><code class="js plain">(self.target) {</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">[self.target performSelector:self.selector withObject:timer.userInfo];</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">} </code></code><code class="js keyword"><code class="js keyword">else</code></code> <code class="js plain"><code class="js plain">{</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">[self.timer invalidate];</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">}</code></code><code class="js plain"><code class="js plain">}</code></code><code class="js plain"><code class="js plain">@end</code></code></td> </tr> </tbody> </table><p><p>Then we encapsulate a fake scheduledtimerwithtimeinterval method, but it's already rescue at the time of the Call:</p></p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">123456789101112131415</td> <td class="code"><code class="js plain"><code class="js plain">+ (NSTimer *) scheduledTimerWithTimeInterval:(NSTimeInterval)interval</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">target:(id)aTarget</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">selector:(SEL)aSelector</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">userInfo:(id)userInfo</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">repeats:(BOOL)repeats {</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">HWWeakTimerTarget* timerTarget = [[HWWeakTimerTarget alloc] init];</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">timerTarget.target = aTarget;</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">timerTarget.selector = aSelector;</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">timerTarget.timer = [NSTimer scheduledTimerWithTimeInterval:interval</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">target:timerTarget</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">selector:@selector(fire:)</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">userInfo:userInfo</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">repeats:repeats];</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js keyword"><code class="js keyword">return</code></code> <code class="js plain"><code class="js plain">timerTarget.timer;</code></code><code class="js plain"><code class="js plain">}</code></code></td> </tr> </tbody> </table><p><p>Run again, problem Solved.</p></p><p><p><strong>Block</strong></p></p><p><p>Wouldn't it be better if we could call Nstimer with Block. We can do this by:</p></p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">123456789101112131415161718</td> <td class="code"><code class="js plain"><code class="js plain">+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">block:(HWTimerHandler)block</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">userInfo:(id)userInfo</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">repeats:(BOOL)repeats {</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js keyword"><code class="js keyword">return</code></code><code class="js plain"><code class="js plain">[self scheduledTimerWithTimeInterval:interval</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">target:self</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">selector:@selector(_timerBlockInvoke:)</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">userInfo:@[[block copy], userInfo]</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">repeats:repeats];</code></code><code class="js plain"><code class="js plain">}</code></code><code class="js plain"><code class="js plain">+ (void)_timerBlockInvoke:(NSArray*)userInfo {</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">HWTimerHandler block = userInfo[0];</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">id info = userInfo[1];</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js comments"><code class="js comments">// or `!block ?: block();` @sunnyxx</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js keyword"><code class="js keyword">if</code></code><code class="js plain"><code class="js plain">(block) {</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">block(info);</code></code><code class="js spaces"><code class="js spaces"> </code></code><code class="js plain"><code class="js plain">}</code></code><code class="js plain"><code class="js plain">}</code></code></td> </tr> </tbody> </table><p><p>So we can write the logic directly in the Block:</p></p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">123456</td> <td class="code"><code class="js plain">- (IBAction)fireButtonPressed:(id)sender {</code><code class="js spaces"> </code><code class="js plain">_timer = [HWWeakTimer scheduledTimerWithTimeInterval:3.0f block:^(id userInfo) {</code><code class="js spaces"> </code><code class="js plain">NSLog(@</code><code class="js string">"%@"</code><code class="js plain">, userInfo);</code><code class="js spaces"> </code><code class="js plain">} userInfo:@</code><code class="js string">"Fire"</code><code class="js plain">repeats:YES];</code><code class="js spaces"> </code><code class="js plain">[_timer fire];</code><code class="js plain">}</code></td> </tr> </tbody> </table><p><p>well, That's it.</p></p><p><p><strong>More</strong></p></p><p><p>The above code is simply encapsulated in the hwweaktimer, Welcome to the Trial.</p></p><p><p><strong>Reference Documents:</strong></p></p> <ul class=" list-paddingleft-2"> <ul class=" list-paddingleft-2"> <li><p>Nstimer</p></li> <li><p>How to Stop/invalidate Nstimer</p></li> <li><p>Weak Reference to Nstimer Target to Prevent Retain Cycle</p></li> <li><p>Performselector may cause a leak because it selector is unknown</p></li> </ul> </ul><p><p>Nstimer in IOS</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service