【问题标题】:How to implement atomic properties in objective C (with or without ARC)?如何在目标 C 中实现原子属性(有或没有 ARC)?
【发布时间】:2012-05-22 16:48:57
【问题描述】:

这就是我的实现方式。有些东西只是没有填好

-(void)setCurrentAnchor:(CLLocation *)currentAnchor 
{
    //CM(@"set current anchor");
    /*@synchronized (self)
     {

     }*/

    if (_currentAnchor==currentAnchor)
    {
        return;
    }
    //[Tools DoSomethingWithSynchronize:^{
    @synchronized(self){
        _currentAnchor=currentAnchor;
        [Timer searchCriteriaChanged];
        [cachedProperties setDistanceForAllBiz];
    }

    //}];

}

-(CLLocation *)currentAnchor
{
    //[Tools DoSomethingWithSynchronize:^{
    //}];
    @synchronized(self){

    } //Empty @synchronized section just to block every other thread
    [self setCurrentLocationasAnchorifNil];
    return _currentAnchor;
}

目标当然是确保 currentAnchor 在更改时永远不会被访问。我这样做对吗?

【问题讨论】:

标签: objective-c multithreading xcode4.3 synchronized


【解决方案1】:

最好使用死的简单 getter/setter 实现——最好是 @synthesize——并将所有更改响应逻辑移到 getter/setter 之外。如果您需要单个 getter/setter 响应逻辑,KVO 可以正常工作。如果您想批量响应多个属性更改,则必须有一个外部事务机制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 2018-06-10
    相关资源
    最近更新 更多