2012-05-22 23 views
0

這是我如何實施我的。 Sometihng只是不能正確填寫如何在目標C中實現原子屬性(有或沒有ARC)?

-(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在更改時不會被訪問。我做對了嗎?

+0

不,不是真的。請參閱[原子與非原子屬性](http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties/589348#589348) –

回答

0

使用死亡簡單的getter/setter實現 - 理想情況下爲@synthesize - 並將getter/setter之外的所有change-response邏輯移開。如果您需要單個getter/setter響應邏輯,KVO可以正常工作。如果您想批量處理多個屬性更改,則必須具有外部交易機制。

相關問題