2012-10-20 22 views
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); 
} 

回答

0

对于你可以在属性设置为原子简单的原语,和你没有在线程之间不用担心读写不一致。

对于指针,除了将属性设置为原子以外,还应使用@synchronize以避免读写一致性。另外请注意,如果你的计时器与你的其他代码(在主线程+ runloop上)在同一线程中,你不需要做任何事情,因为计时器事件将被同一个runloop触发主线程代码的其余部分,并不会真正并发。

+0

重新阅读Apple文档后,我想我明白如果从'sharedItem'中删除nonatomic属性,它应该可以正常工作。 [..]指定strong,copy或retain并且不指定nonatomic,那么在引用计数环境中,对象属性的合成get访问器使用锁并保留并自动释放返回的值。如果指定nonatomic,则对象属性的合成访问器只是直接返回值。 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html –

+0

正确但只保护非指针原始类型值,它不保护包含在该属性指向的对象。这就是为什么你需要@synchronize从多个线程变异的对象。 –

+0

我怀疑dispatch_source甚至在一个单独的线程上运行,所以如果你没有其他线程运行其他代码不担心它。 –

相关问题