Reputation: 2279
I'm having problems with Core Data
concurrency on my iOS app.
On my executeFetchRequest
I tried to synchronize the managedObjectContext
request, but some times this method makes my app freeze.
- (NSArray *)synchronizedWithFetchRequest:(NSFetchRequest *)request andError:(NSError **)error
{
@synchronized(self.managedObjectContext)
{
return [self.managedObjectContext executeFetchRequest:request error:error];
}
}
I've already tried many things like lock
/unlock
, performBlock
/performBlockAndWait
, dispatch_sync
/dispatch_async
and nothing seems to work.
Managed Object Context creation:
...
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator];
Is there some way around this? and keep my request returning the results objects on this method?
Thanks!
Upvotes: 1
Views: 1149
Reputation: 6011
Synchronising on the MOC suggests that there is more than one thread accessing the same MOC.
That in itself is a violation of CoreData concurrency protocols.
This access is prohibited unless it is wrapped in the context performBlock:
method (or its "wait" counterpart). this will negate the need for the @synchronized
block altogether.
This thread/queue "boundness" extends to the contexts fetched/registered managed objects, and so, you will not be able to access them as the return values of your method.
Upvotes: 2