如何知道何时使 `NSTimer` 失效

6

以下是我遇到的问题:

我有一个模型类,在其中有一个 NSTimer,我希望该定时器在整个模型对象的生命周期内运行。初始化很容易:我只需在 init 方法中加入以下代码:

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

然而,我的问题是,当模型从内存中释放时,我该如何使此计时器失效? 通常情况下,这很容易解决,但据我所知,当您安排一个NSTimer时,操作系统会维护对计时器对象的强引用。

我该怎么办?是否有一种方法在模型从内存中释放之前调用它?


1
我以前从未真正使用过这个...当我学习Objective-C时,总是被告知dealloc已经很少使用了。我的属性在dealloc方法中仍然有效吗? - Nosrettap
如何处理dealloc?是的,它们会被释放。我正在将其输入作为答案。 - s.bandara
太酷了!如果你把这个作为答案发表,我会接受它。 - Nosrettap
检查其他人的评论。你将不得不采用不同的策略。 - s.bandara
每当您完成对象时,可能需要手动使计时器失效。只要计时器在运行,它就不会被释放。 - iDev
2个回答

22

[NSTimer scheduledTimerWithTimeInterval:...]方法会保留目标对象,所以如果目标对象是self,那么该模型类的实例将永远不会被释放。

解决方法是使用一个单独的对象(在下面的示例中称为TimerTarget)。TimerTargetModelClass使用弱引用,以避免保留循环。

这个“辅助类”长这样。它的唯一目的是将定时器事件转发到“真正的目标”。

@interface TimerTarget : NSObject
@property(weak, nonatomic) id realTarget;
@end

@implementation TimerTarget

- (void)timerFired:(NSTimer*)theTimer
{
    [self.realTarget performSelector:@selector(timerFired:) withObject:theTimer];
}

@end

现在,在您的模型类中,您可以创建一个计时器并在dealloc中使其失效:

@interface ModelClass ()
@property(strong, nonatomic) NSTimer *timer;
@end

@implementation ModelClass

- (id)init
{
    self = [super init];
    if (self) {
        TimerTarget *timerTarget = [[TimerTarget alloc] init];
        timerTarget.realTarget = self;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                 target:timerTarget
                                               selector:@selector(timerFired:)
                                               userInfo:nil repeats:YES];
    }
    return self;
}

- (void)dealloc
{
    [self.timer invalidate]; // This releases the TimerTarget as well!
    NSLog(@"ModelClass dealloc");
}

- (void)timerFired:(NSTimer*)theTimer
{
    NSLog(@"Timer fired");
}

@end

所以我们有

modelInstance ===> timer ===> timerTarget ---> modelInstance
(===> : strong reference, ---> : weak reference)

请注意,定时器不再对模型类实例有(强)引用。

我已经通过以下代码进行了测试,它创建了一个ModelClass的实例并在5秒后释放:

__block ModelClass *modelInstance = [[ModelClass alloc] init];
int64_t delayInSeconds = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    modelInstance = nil;
});

输出:

2013-01-23 23:54:11.483 timertest[16576:c07] Timer fired
2013-01-23 23:54:12.483 timertest[16576:c07] Timer fired
2013-01-23 23:54:13.483 timertest[16576:c07] Timer fired
2013-01-23 23:54:14.483 timertest[16576:c07] Timer fired
2013-01-23 23:54:15.483 timertest[16576:c07] Timer fired
2013-01-23 23:54:15.484 timertest[16576:c07] ModelClass dealloc

2
+1,由于没有其他简单的方法来做到这一点,我认为在这种情况下这将是更好的选择。 - iDev
3
可以用自定义的 AutoreleasingTimer 类来封装这个功能。 - Martin R
根据苹果文档,我们“不应尝试子类化NSTimer”。也许这样一个AutoreleasingTimer类可以有工厂方法,返回其目标设置为内部AutoreleasingTimerTarget类的NSTimer对象? - David Schwartz
1
@DavidSchwartz:你说得对,我并不是指要子类化NSTimer。 - Martin R
1
啊,好的 - 在重新阅读了您的评论后,我意识到您从未建议子类化NSTimer。非常抱歉。 - David Schwartz
我在想这是否真的是唯一的方法。为什么苹果不提供其他东西,或者在处理紧邻 NSTimer 的时序时是否有更好的方法? - Alex Cio

0

基于 @Martin R 的想法,我创建了一个自定义类,更易于使用,并添加了一些检查以避免崩溃。

@interface EATimerTarget : NSObject

// Initialize with block to avoid missing call back
- (instancetype)initWithRealTarget:(id)realTarget timerBlock:(void(^)(NSTimer *))block;

// For NSTimer @selector() parameter
- (void)timerFired:(NSTimer *)timer;

@end

@interface EATimerTarget ()
@property (weak, nonatomic) id realTarget;
@property (nonatomic, copy) void (^timerBlock)(NSTimer *); // use 'copy' to avoid retain counting
@end

@implementation EATimerTarget

- (instancetype)initWithRealTarget:(id)realTarget timerBlock:(void (^)(NSTimer *))block {
    self = [super init];
    if (self) {
        self.realTarget = realTarget;
        self.timerBlock = block;
    }
    return self;
}

- (void)timerFired:(NSTimer *)timer {
    // Avoid memory leak, timer still run while our real target is dealloc
    if (self.realTarget) {
        self.timerBlock(timer);
    }
    else {
        [timer invalidate];
    }
}

@end

这是我的示例类
@interface MyClass
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation MyClass

- (id)init
{
    self = [super init];
    if (self) {
         // Using __weak for avoiding retain cycles
         __weak typeof(self) wSelf = self;
    EATimerTarget *timerTarget = [[EATimerTarget alloc] initWithRealTarget:self timerBlock: ^(NSTimer *timer) {
        [wSelf onTimerTick:timer];
    }];
         self.timer = [NSTimer timerWithTimeInterval:1 target:timerTarget selector:@selector(timerFired:) userInfo:nil repeats:YES];
         [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    }
    return self;
}

- (void)dealloc
{
    [self.timer invalidate]; // This releases the EATimerTarget as well! 
    NSLog(@"### MyClass dealloc");
}

- (void)onTimerTick:(NSTimer *)timer {
     // DO YOUR STUFF!
     NSLog(@"### TIMER TICK");
}

@end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接