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.