When using the NSPrivateQueueConcurrencyType and NSMainQueueConcurrencyType for NSManagedObjectContext , is it safe to make nested executeBlock calls in the same context?
[backgroundContext performBlock:^{ NSFetchRequest *myRequest = ...; __block NSArray *result= nil; [backgroundContext performBlockAndWait:^{ results = [backgroundContext executeFetchRequest:myRequest error:NULL]; }]; }];
This may sound silly, but I have an existing code base with lots of helper methods that encapsulate executeFetchRequest calls. I do not want to make assumptions that the caller has already used executeBlock or not. For example:
-(void)updateObjects:(BOOL)synchronous { if (YES == synchronous) [self fetchHelper]; else { [backgroundContext performBlock:^{ [self fetchHelper]; }]; } } -(NSArray*)fetchHelper { [self.backgroundContext performBlockAndWait:^{
I tried and it works. But I learned (the hard way) to be very careful with Core Data and multi-threaded.
ios core-data nsmanagedobjectcontext
FKDev
source share