Prevents nstimer retain from serving as the target's self

Source: Internet
Author: User

First, let's vomit this title. It's a little bit of space. It doesn't look like Chinese, but it looks more strange if you don't write a space. Please find a solution ......

 

Nstimer will rehold its target, so if a nstimer is defined in the controller and the target is set to self, it will cause loop reference.

The solution is the same as preventing block references from self. The first step is to encapsulate the nstimer action into a block. The second step is to pass a weak reference of self to the block.

 

First, define a nstimer category:

 1 #import <Foundation/Foundation.h> 2  3 @interface NSTimer (BlockSupport) 4  5 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats; 6  7 @end 8  9 #import "NSTimer+BlockSupport.h"10 11 @implementation NSTimer (BlockSupport)12 13 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats {14     return [self scheduledTimerWithTimeInterval:interval target:self selector:@selector(blockInvoke:) userInfo:[block copy] repeats:repeats];15 }16 17 + (void)blockInvoke:(NSTimer *)timer {18     void (^block)() = timer.userInfo;19     if (block) {20         block();21     }22 }23 24 @end

This category supports creating nstimer using blocks, passing the operation to the omnipotent object userinfo, and then creating the nstimer in the Controller as follows:

1     __block typeof(self) weakSelf = self;2     _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 block:^{3         [weakSelf doSth];4     } repeats:YES];

In this way, nstimer will not make the reference count of the controller + 1.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.