分享

NSTimer和实现弱引用的timer的方式

 最初九月雪 2018-03-30

NikonPicTNW.jpg

本文是投稿文章,作者:yohunl 


目录

  • 我们常用NSTimer的方式

  • 上面的NSTimer无论采用何种方式都是在主线程上跑的那么怎么在非主线程中跑一个NSTimer呢

  • GCD的方式

  • 一次性的timer方式的GCD模式

  • 另一种dispatch_after方式的定时器

  • 利用GCD的弱引用型的timer

  • 使用NSTimer方式创建的Timer使用时候需要注意

  • 参考文档

  • 我们常用NSTimer的方式

如下代码所示,是我们最常见的使用timer的方式

1
2
3
4
5
@property (nonatomic , strong) NSTimer *animationTimer;self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:(self.animationDuration = animationDuration)
                                                               target:self
                                                             selector:@selector(animationTimerDidFired:)
                                                             userInfo:nil
                                                              repeats:YES];123456

当使用NSTimer的scheduledTimerWithTimeInterval方法时。事实上此时Timer会被加入到当前线程的Run Loop中,且模式是默认的NSDefaultRunLoopMode。而如果当前线程就是主线程,也就是UI线程时,某些UI事件,比如UIScrollView的拖动操作,会将Run Loop切换成NSEventTrackingRunLoopMode模式,在这个过程中,默认的NSDefaultRunLoopMode模式中注册的事件是不会被执行的。也就是说,此时使用scheduledTimerWithTimeInterval添加到Run Loop中的Timer就不会执行。
我们可以通过添加一个UICollectionView,然后滑动它后打印定时器方法

1
2
3
4
5
6
7
2016-01-27 11:41:59.770 TimerAbout[89719:1419729] enter timer
2016-01-27 11:42:00.339 TimerAbout[89719:1419729] enter timer
2016-01-27 11:42:01.338 TimerAbout[89719:1419729] enter timer
2016-01-27 11:42:02.338 TimerAbout[89719:1419729] enter timer
2016-01-27 11:42:03.338 TimerAbout[89719:1419729] enter timer
2016-01-27 11:42:15.150 TimerAbout[89719:1419729] enter timer
2016-01-27 11:42:15.338 TimerAbout[89719:1419729] enter timer

从中可以看到,当UICollectionView滑动时候,定时器方法并没有打印(从03.338到15.150)

为了设置一个不被UI干扰的Timer,我们需要手动创建一个Timer,然后使用NSRunLoop的addTimer:forMode:方法来把Timer按照指定模式加入到Run Loop中。这里使用的模式是:NSRunLoopCommonModes,这个模式等效于NSDefaultRunLoopMode和NSEventTrackingRunLoopMode的结合,官方参考文档

还是上面的例子,换为:

1
2
self.animationTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(animationTimerDidFired:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.animationTimer forMode:NSRunLoopCommonModes];12

则,无论你滑动不滑动UICollectionView,定时器都是起作用的!

上面的NSTimer无论采用何种方式,都是在主线程上跑的,那么怎么在非主线程中跑一个NSTimer呢?

我们简单的可以使用如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
//创建并执行新的线程
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(newThread) object:nil];
    [thread start];
     
- (void)newThread
{
    @autoreleasepool
    {        //在当前Run Loop中添加timer,模式是默认的NSDefaultRunLoopMode
        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(animationTimerDidFired:) userInfo:nil repeats:YES];        //开始执行新线程的Run Loop
        [[NSRunLoop currentRunLoop] run];
    }
}1234567891011121314

当然了,因为是开启的新的线程,在定时器的回调方法中,需要切换到主线程才能操作UI。

GCD的方式

1
2
3
4
5
6
7
8
9
10
11
12
//GCD方式
    uint64_t interval = 1 * NSEC_PER_SEC;
    //创建一个专门执行timer回调的GCD队列
    dispatch_queue_t queue = dispatch_queue_create("timerQueue", 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //使用dispatch_source_set_timer函数设置timer参数
    dispatch_source_set_timer(_timer, dispatch_time(DISPATCH_TIME_NOW, 0), interval, 0);
    //设置回调
    dispatch_source_set_event_handler(_timer, ^(){
        NSLog(@"Timer %@", [NSThread currentThread]);
    });
    dispatch_resume(_timer);//dispatch_source默认是Suspended状态,通过dispatch_resume函数开始它123456789101112

其中的dispatch_source_set_timer的最后一个参数,是最后一个参数(leeway),它告诉系统我们需要计时器触发的精准程度。所有的计时器都不会保证100%精准,这个参数用来告诉系统你希望系统保证精准的努力程度。如果你希望一个计时器每5秒触发一次,并且越准越好,那么你传递0为参数。另外,如果是一个周期性任务,比如检查email,那么你会希望每10分钟检查一次,但是不用那么精准。所以你可以传入60,告诉系统60秒的误差是可接受的。他的意义在于降低资源消耗。

一次性的timer方式的GCD模式

1
2
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        NSLog(@"dispatch_after enter timer");
    });123

另一种dispatch_after方式的定时器

这个是使用上面的dispatch_after来创建的,通过递归调用来实现。

1
2
3
4
5
6
7
- (void)dispatechAfterStyle {
    __weak typeof (self) wself = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"dispatch_after enter timer,thread = %@", [NSThread currentThread]);
        [wself dispatechAfterStyle];
    });
}

利用GCD的弱引用型的timer

MSWeaker实现了一个利用GCD的弱引用的timer。原理是利用一个新的对象,在这个对象中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
NSString *privateQueueName = [NSString stringWithFormat:@"com.mindsnacks.msweaktimer.%p", self];
        self.privateSerialQueue = dispatch_queue_create([privateQueueName cStringUsingEncoding:NSASCIIStringEncoding], DISPATCH_QUEUE_SERIAL);
        dispatch_set_target_queue(self.privateSerialQueue, dispatchQueue);
        self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
                                            0,
                                            0,
                                            self.privateSerialQueue);
                                             
- (void)resetTimerProperties
{
    int64_t intervalInNanoseconds = (int64_t)(self.timeInterval * NSEC_PER_SEC);
    int64_t toleranceInNanoseconds = (int64_t)(self.tolerance * NSEC_PER_SEC);
    dispatch_source_set_timer(self.timer,
                              dispatch_time(DISPATCH_TIME_NOW, intervalInNanoseconds),
                              (uint64_t)intervalInNanoseconds,
                              toleranceInNanoseconds
                              );
}
- (void)schedule
{
    [self resetTimerProperties];
     
    __weak MSWeakTimer *weakSelf = self;
     
    dispatch_source_set_event_handler(self.timer, ^{
        [weakSelf timerFired];
    });
     
    dispatch_resume(self.timer);
}

创建了一个队列self.timer = dispatch_source_create,然后在这个队列中创建timer dispatch_source_set_timer.

注意其中用到了dispatch_set_target_queue(self.privateSerialQueue, dispatchQueue); 这个是将dispatch队列的执行操作放到队列dispatchQueue 中去。

这份代码中还用到了原子操作!值得好好研读,以便以后可以在自己的多线程设计中使用原子操作。
为什么用原子操作呢,因为作者想的是在多线程的环境下设置定时器的开关与否。

1
2
3
4
5
6
7
8
9
10
if (OSAtomicAnd32OrigBarrier(1, &_timerFlags.timerIsInvalidated))
if (!OSAtomicTestAndSet(7, &_timerFlags.timerIsInvalidated))
    {
        dispatch_source_t timer = self.timer;
        dispatch_async(self.privateSerialQueue, ^{
            dispatch_source_cancel(timer);
            ms_release_gcd_object(timer);
        });
    }

至于其中

1
2
3
4
 struct
    {
        uint32_t timerIsInvalidated;
    } _timerFlags;

这里为什么要用结构体呢?为什么不直接使用一个uint32_t 的变量?

使用NSTimer方式创建的Timer,使用时候需要注意。

由于

1
2
3
4
5
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                           target:self
                                                         selector:@selector(animationTimerDidFired:)
                                                         userInfo:nil
                                                          repeats:YES];

会导致timer 强引用 self,而animationTimer又是self的一个强引用,这造成了强引用的循环了。 
如果不手工停止timer,那么self这个VC将不能够被释放,尤其是当我们这个VC是push进来的时候,pop将不会被释放!!! 
怎么解决呢?
当然了,可以采用上文提到的MSWeakerGCD的弱引用的timer

可是如果有时候,我们不想使用它,觉得它有点复杂呢?

1.在VC的disappear方法中应该调用 invalidate方法,将定时器释放掉,这里可能有人要说了,我直接在vc的dealloc中释放不行么?

1
2
3
-(void)dealloc {
[_animationTimer invalidate];
}

很遗憾的告诉你,都已经循环引用了,vc压根就释放不了,怎么调dealloc方法?

在vc的disappear方法中

1
2
3
4
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_animationTimer invalidate];
}

这样的确能解决问题,可是不一定是我们想要的呀,当我们vc 再push了一个新的页面的时候,本身vc没有释放,按理说,其成员timer不应该被释放呀,你可能会说,那还不容易,在appear方法中再重新生成一下呗…但是这样的话,又要增加一个变量,标识定时器在上一次disappear时候是不是启动了吧,是启动了,被invaliate的时候,才能在appear中重新启动吧。这样是不是觉得很麻烦?

3.你可能会说,那简单啊,直接若引用就可以了想想我们使用block的时候

1
2
3
4
5
@property (nonatomic, copy) void  (^ myblock)(NSInteger i);
__weak typeof (self) weakSelf = self;
self.myblock = ^(NSInteger i){
    [weakSelf view];
};

在其中,我们需要在block中引用self,如果直接引用,也是循环引用了,采用先定义一个weak变量,然后在block中引用weak对象,避免循环引用 你会直接想到如下的方式

1
2
3
4
5
6
__weak typeof (self) wself = self;
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                       target:wself
                                                     selector:@selector(animationTimerDidFired:)
                                                     userInfo:nil
                                                      repeats:YES];

是不是瞬间觉得完美了,呵呵,我只能说少年,你没理解两者之间的区别。在block中,block是对变量进行捕获,意思是对使用到的变量进行拷贝操作,注意是拷贝的不是对象,而是变量自身。拿上面的来说,block中只是对变量wself拷贝了一份,也就是说,block中也定义了一个weak对象,相当于,在block的内存区域中,定义了一个__weak blockWeak对象,然后执行了blockWeak = wself;注意到了没,这里并没有引起对象的持有量的变化,所以没有问题,再看timer的方式,虽然你是将wself传入了timer的构造方法中,我们可以查看NSTimer的

1
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats1

定义,其target的说明The object to which to send the message specified by aSelector when the timer fires. The timer maintains a strong reference to this object until it (the timer) is invalidated,是要强应用这个变量的 也就是说,大概是这样的,__strong strongSelf = wself 强引用了一个弱应用的变量,结果还是强引用,也就是说strongSelf持有了wself所指向的对象(也即是self所只有的对象),这和你直接传self进来是一样的效果,并不能达到解除强引用的作用!看来只能换个思路了,我直接生成一个临时对象,让Timer强用用这个临时对象,在这个临时对象中弱引用self,可以了吧。

4.考虑引入一个对象,在这个对象中弱引用self,然后将这个对象传递给timer的构建方法 这里可以参考YYWeakProxy建立这个对象:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@interface YYWeakProxy : NSProxy
@property (nonatomic, weak, readonly) id target;
- (instancetype)initWithTarget:(id)target;
+ (instancetype)proxyWithTarget:(id)target;
@end
@implementation YYWeakProxy
- (instancetype)initWithTarget:(id)target {
_target = target;
return self;
}
+ (instancetype)proxyWithTarget:(id)target {
return [[YYWeakProxy alloc] initWithTarget:target];
}
//当不能识别方法时候,就会调用这个方法,在这个方法中,我们可以将不能识别的传递给其它对象处理
//由于这里对所有的不能处理的都传递给_target了,所以methodSignatureForSelector和forwardInvocation不可能被执行的,所以不用再重载了吧
//其实还是需要重载methodSignatureForSelector和forwardInvocation的,为什么呢?因为_target是弱引用的,所以当_target可能释放了,当它被释放了的情况下,那么forwardingTargetForSelector就是返回nil了.然后methodSignatureForSelector和forwardInvocation没实现的话,就直接crash了!!!
//这也是为什么这两个方法中随便写的!!!
- (id)forwardingTargetForSelector:(SEL)selector {
return _target;
}
- (void)forwardInvocation:(NSInvocation *)invocation {
void *null = NULL;
[invocation setReturnValue:&null];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
return [NSObject instanceMethodSignatureForSelector:@selector(init)];
}
- (BOOL)respondsToSelector:(SEL)aSelector {
return [_target respondsToSelector:aSelector];
}
- (BOOL)isEqual:(id)object {
return [_target isEqual:object];
}
- (NSUInteger)hash {
return [_target hash];
}
- (Class)superclass {
return [_target superclass];
}
- (Class)class {
return [_target class];
}
- (BOOL)isKindOfClass:(Class)aClass {
return [_target isKindOfClass:aClass];
}
- (BOOL)isMemberOfClass:(Class)aClass {
return [_target isMemberOfClass:aClass];
}
- (BOOL)conformsToProtocol:(Protocol *)aProtocol {
return [_target conformsToProtocol:aProtocol];
}
- (BOOL)isProxy {
return YES;
}
- (NSString *)description {
return [_target description];
}
- (NSString *)debugDescription {
return [_target debugDescription];
}
@end

使用的时候,将原来的替换为:

1
2
3
4
5
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                       target:[YYWeakProxy proxyWithTarget:self ]
                                                     selector:@selector(animationTimerDidFired:)
                                                     userInfo:nil
                                                      repeats:YES];

5.block方式来解决循环引用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@interface NSTimer (XXBlocksSupport)
+ (NSTimer *)xx_scheduledTimerWithTimeInterval:(NSTimeInterval)interval
                                     block:(void(^)())block
                                   repeats:(BOOL)repeats;
@end
@implementation NSTimer (XXBlocksSupport)
+ (NSTimer *)xx_scheduledTimerWithTimeInterval:(NSTimeInterval)interval
                                     block:(void(^)())block
                                   repeats:(BOOL)repeats
{
return [self scheduledTimerWithTimeInterval:interval
                                      target:self
                                    selector:@selector(xx_blockInvoke:)
                                    userInfo:[block copy]
                                     repeats:repeats];
}
+ (void)xx_blockInvoke:(NSTimer *)timer {
void (^block)() = timer.userinfo;
if(block) {
    block();
}
}
@end

注意:以上NSTimer的target是NSTimer类对象,类对象本身是个单利,此处虽然也是循环引用,但是由于类对象不需要回收,所以没有问题。但是这种方式要注意block的间接循环引用,当然了,解决block的间接循环引用很简单,定义一个weak变量,在block中使用weak变量即可。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多