美文网首页微信小程序开发
NSTimer循环引用解决方案

NSTimer循环引用解决方案

作者: 你买票吗 | 来源:发表于2019-11-25 15:27 被阅读0次

    文章以在TimerViewController中使用计时器为例,在VC中声明一个NSTimer属性。

    创建NSTimer对象:

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(startTimer) userInfo:nil repeats:YES];
    
    

    timer作为VC的属性,被VC强引用,创建timer对象时VC作为target被timer强引用,即循环引用。

    image

    解决循环引用的几种方式:

    weak指针:

    既然是强引用导致循环引用,那么用__weak修饰self就好了,想法是对的,但是做法是无效的。因为无论是weak还是strong修饰,在NSTimer中都会重新生成一个新的强引用指针指向self,导致循环引用的。

    类方法:

    创建一个继承NSObject的子类TempTarget,并创建开启计时器的方法。

    TempTarget .h文件

    @property (nonatomic, assign) SEL selector;
    @property (nonatomic, weak) NSTimer *tempTimer;
    @property (nonatomic, weak) id tempTarget;
    
    + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval target:(id)tempTarget selector:(SEL)selector userInfo:(id)userInfo repeats:(BOOL)repeats;
    
    

    TempTarget .m文件

    @implementation TempTarget
    
    + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval target:(id)tempTarget selector:(SEL)selector userInfo:(id)userInfo repeats:(BOOL)repeats {
        TempTarget *target = [[TempTarget alloc] init];
        target.tempTarget = tempTarget;
        target.selector = selector;
        target.tempTimer = [NSTimer scheduledTimerWithTimeInterval:interval target:target selector:@selector(timerSelector:) userInfo:userInfo repeats:repeats];
        return target.tempTimer;
    }
    
    - (void)timerSelector:(NSTimer *)tempTimer {
        if (self.tempTarget && [self.tempTarget respondsToSelector:self.selector]) {
            [self.tempTarget performSelector:self.selector withObject:tempTimer.userInfo];
        }else {
            [self.tempTimer invalidate];
        }
    }
    
    @end
    
    

    VC调用

    self.timer = [TempTarget scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerStart:) userInfo:nil repeats:YES];
    
    

    VC强引用timer,因为timer的target是TempTarget实例,所以timer强引用TempTarget实例,而TempTarget实例弱引用VC,解除循环引用。

    image
    WeakProxy:

    创建一个继承NSProxy的子类WeakProxy,并实现消息转发的相关方法。

    WeakProxy.h文件

    @property (nonatomic, weak, readonly) id weakTarget;
    
    + (instancetype)proxyWithTarget:(id)target;
    - (instancetype)initWithTarget:(id)target;
    
    

    WeakProxy.m文件

    @implementation WeakProxy
    
    + (instancetype)proxyWithTarget:(id)target {
        return [[self alloc] initWithTarget:target];
    }
    
    - (instancetype)initWithTarget:(id)target {
        _weakTarget = target;
        return self;
    }
    
    - (void)forwardInvocation:(NSInvocation *)invocation {
        SEL sel = [invocation selector];
        if ([self.weakTarget respondsToSelector:sel]) {
            [invocation invokeWithTarget:self.weakTarget];
        }
    }
    
    //返回方法的签名。
    - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
        return [self.weakTarget methodSignatureForSelector:sel];
    }
    
    - (BOOL)respondsToSelector:(SEL)aSelector {
        return [self.weakTarget respondsToSelector:aSelector];
    }
    @end
    
    

    VC调用

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:[WeakProxy proxyWithTarget:self] selector:@selector(timerStart:) userInfo:nil repeats:YES];
    - (void)dealloc {
        [_timer invalidate];
        NSLog(@"VC销毁了");
    }
    
    

    原理跟类方法相似,打破循环引用的环路。将timer的target设置为WeakProxy实例,利用消息转发机制实现执行VC中的计时方法,解决循环引用。

    GCD:

    VC的.m文件

    @interface ViewController ()
    @property (nonatomic, strong) dispatch_source_t timer;
    @end
    
    - (void)viewDidLoad {
        [super viewDidLoad];
       dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        dispatch_source_set_timer(_timer, dispatch_time(DISPATCH_TIME_NOW, 0), 0.1*NSEC_PER_SEC, 0);
        dispatch_source_set_event_handler(_timer, ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"执行了");
            });
        });
        //开启计时器
        dispatch_resume(_timer);
    }
    
    

    //结束计时
    dispatch_source_cancel(_timer);

    Block:

    创建NSTimer的分类NSTimer+UnretainCycle。

    + (NSTimer *)lhScheduledTimerWithTimeInterval:(NSTimeInterval)inerval
                                          repeats:(BOOL)repeats
                                            block:(void(^)(NSTimer *timer))block {
        return [NSTimer scheduledTimerWithTimeInterval:inerval target:self selector:@selector(blcokInvoke:) userInfo:[block copy] repeats:repeats];
    }
    
    + (void)blcokInvoke:(NSTimer *)timer {
    
        void (^block)(NSTimer *timer) = timer.userInfo;
        if (block) {
            block(timer);
        }
    }
    
    

    VC调用

    self.timer = [NSTimer mxScheduledTimerWithTimeInterval:0.5 repeats:YES block:^(NSTimer *timer) {
            NSLog(@"执行了");
        }];
    
    

    来自:https://www.jianshu.com/p/aaf7b13864d9

    相关文章

      网友评论

        本文标题:NSTimer循环引用解决方案

        本文链接:https://www.haomeiwen.com/subject/uycjwctx.html