0
我想提供对定时器事件处理程序的类属性的读/写访问权限,但也会在事件处理程序外的类中的其他位置更新相同的属性。我需要采取哪些预防措施来确保正在读取的数据正确&已更新?访问定时器事件处理程序中的可变属性
这里的一般逻辑:
// declared in the class header and initialized to 1 in init
@property (nonatomic, strong) NSNumber *sharedItem;
@property (nonatomic, assign) dispatch_source_t timer;
// Method invoked independent of the timer
- (void)doSomeWork {
// QUESTION: During a timer tick, will it access the correct version of
// sharedItem that is updated here?
// Do I need to protect this area with a critical section/lock?
sharedItem = [NSNumber numberWithInteger:[sharedItem intValue] + 1];
}
- (void)myTimerRelatedMethod {
// Creating the timer
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(self.timer, startTime, interval, leeway);
// Timer's event handler run for each tick
dispatch_source_set_event_handler(self.timer, ^{
if ([sharedItem intValue] > 10) {
// 1. Do something
// 2. Then cancel the timer
}
});
dispatch_resume(self.timer);
}
重新阅读Apple文档后,我想我明白如果从'sharedItem'中删除nonatomic属性,它应该可以正常工作。 [..]指定strong,copy或retain并且不指定nonatomic,那么在引用计数环境中,对象属性的合成get访问器使用锁并保留并自动释放返回的值。如果指定nonatomic,则对象属性的合成访问器只是直接返回值。 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html –
正确但只保护非指针原始类型值,它不保护包含在该属性指向的对象。这就是为什么你需要@synchronize从多个线程变异的对象。 –
我怀疑dispatch_source甚至在一个单独的线程上运行,所以如果你没有其他线程运行其他代码不担心它。 –