それは私にとって非常に理にかなっていたので、私はPaul de Langeの答えに行きました. @synchronized
プロパティを設定してブロックにラップするすべての場所を見つける必要はありませんでした。
同様のことをする必要がある他の人のために、私が思いついたコードは次のとおりです。
@interface MyClass {
//...
//The iVar, note that the iVar has a different name than the property, see the @synthesize command
RefreshOperation *_refreshOperation;
//...
}
@property (nonatomic, retain) RefreshOperation *refreshOperation;
@implementation MyClass
//...
//The timer's callback
- (void)statusTimerEvent:(NSTimer *)aTimer
{
//Block/wait for thread safe setters/getters
@synchronized(self)
{
if (self.refreshOperation)
{
self.status = [self.refreshOperation status];
progressView.progress = self.refreshOperation.progress;
}
}
}
//...
//Thread safe setter for the refreshOperation property
- (void)setRefreshOperation:(RefreshOperation *)refreshOperation:(RefreshOperation *)newRefreshOperation
{
@synchronized(self)
{
if (_refreshOperation != newRefreshOperation)
{
[_refreshOperation release];
_refreshOperation = [newRefreshOperation retain];
}
}
}
//Thread safe getter for the refreshOperation property
- (RefreshOperation *)refreshOperation
{
id result = nil;
@synchronized(self)
{
result = [_refreshOperation retain];
}
return [result autorelease];
}
//...
- (void)dealloc
{
//...
self.refreshOperation = nil;
[super dealloc];
}
//...
//Connect the property |refreshOperation| to the iVar |_refreshOperation|; having the same name for both results in warning about the local variable hiding a property
@synthesize refreshOperation = _refreshOperation;