Wrapping network thread lazy initializer with dispatch_once, to ensure thread safety

This commit is contained in:
Mattt Thompson 2011-08-31 09:59:34 -05:00
parent c0271e6156
commit a167217ee6

View file

@ -108,10 +108,12 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
static NSThread *_networkRequestThread = nil; static NSThread *_networkRequestThread = nil;
+ (NSThread *)networkRequestThread { + (NSThread *)networkRequestThread {
if (!_networkRequestThread) { static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_networkRequestThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkRequestThreadEntryPoint:) object:nil]; _networkRequestThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkRequestThreadEntryPoint:) object:nil];
[_networkRequestThread start]; [_networkRequestThread start];
} });
return _networkRequestThread; return _networkRequestThread;
} }