When using NSPrivateQueueConcurrencyType
and NSMainQueueConcurrencyType
types for NSManagedObjectContext
,
is it safe to make nested performBlock calls on the same context ?
[backgroundContext performBlock:^{
NSFetchRequest *myRequest = ...;
__block NSArray *result= nil;
[backgroundContext performBlockAndWait:^{
results = [backgroundContext executeFetchRequest:myRequest error:NULL];
}];
}];
It may seem stupid but I have an existing codebase with a lot of helpers methods which encapsulate the executeFetchRequest
calls. I don't want to make assumptions about whether the caller has already used performBlock or not.
For example:
-(void)updateObjects:(BOOL)synchronous
{
if (YES == synchronous)
[self fetchHelper];
else
{
[backgroundContext performBlock:^{
[self fetchHelper];
}];
}
}
-(NSArray*)fetchHelper
{
[self.backgroundContext performBlockAndWait:^{
//Fetch the objects...
[self.backgroundContext executeFetchRequest: (...)];
}];
}
I have tried it and it works. But I have learned (the hard way) to be very careful with Core Data and multi-threading.