为什么这个完成块被正确调用?

Why is this completion block correctly being called?

为什么这个完成块会被正确调用?

CompletionBlock comp = ^(BOOL enabled) {
            //enabled is being correctly set
            NSLog(@"result: %i", enabled);
};

@autoreleasepool {
    Monitor *monitor = [[Monitor alloc]initWithCompletionBlock:comp];
    monitor = nil;
}

监视器会在监视器实例化10秒后启动一个NSTimer来调用完成块。

监视器确实在内部引用了 comp(通过 @property (nonatomic) CompletionBlock compblock;),但是循环引用的成分似乎不存在,因为没有引用监视器。

这是正常现象吗?这是我可以指望的行为吗?这对我来说真的没有意义。

大概Monitor实例是这里的目标。 NSTimer 保留其目标以避免在触发时向已释放的对象发送消息而导致崩溃。*当前的 运行 循环在计划时保留计时器。

这是正常行为,you can rely on it

target
The object to which to send the message [...] The timer maintains a strong reference to target...

事实上,您必须考虑到这一点,以免在计时器和它的目标之间创建一个保留循环。


*NSTimer 早于自动无效引用很长一段时间。