I am currently handling some setup of an object on a background thread (I am using a concurrent queue within the following initWithDocumentFileURL
method):
+ (TWReaderDocument *)initWithDocumentFileURL:(NSURL *)url withLoadingCompletionBlock:(loadingCompletionBlock)completionBlock{
__block NSError *error;
TWReaderDocument * twDoc = [[TWReaderDocument alloc] init];
twDoc.status = ReaderDocCreated;
twDoc.doc = [ReaderDocument withDocumentFilePath:[url path] withURL:url withLoadingCompletionBLock:^(NSError *_error) {
if (_error)
{
_error = error;
twDoc.status = ReaderDocReady;
}
else twDoc.status = ReaderDocFailed;
}];
//completionBlock(error);
return twDoc;
}
Header
@property (nonatomic, assign, readonly) TWReaderDocumentStatus status;
Implementation
- (TWReaderDocumentStatus) status {
__block TWReaderDocumentStatus readStatus;
dispatch_sync([ReaderDocument concurrentLoadingQueue], ^{
readStatus = _status;
});
return readStatus;
}
If I would like to access the status property on the main thread do I need to take any precaution or there is nothing to take care of as I am just reading the property on the main thread?
_error = error
? I could imagineerror = _error
if you were trying to save theNSError
passed to your block (but then again, the idea of updating a__block
variable from an asynchronous method doesn't make a lot of sense). \$\endgroup\$