pawi pawi - 8 months ago 74
iOS Question

CoreData Multi-Threaded

I’m having trouble with CoreData in a mulit-threaded environment.

I have a RestKit setup, meaning there is a persistentManagedObjectContext (private) of which I create a child-context (self.autoSyncMOC, also private). I’m not touching the mainManagedObjectContext of RestKit (main-thread), which is a child of the persistentMOC (sibling of my autoSyncMOC).

@property (nonatomic, strong) NSManagedObjectContext *autoSyncMOC;

This is the Code that throws the Multithreading_Violation_AllThatIsLeftToUsIsHonor exception:

self.autoSyncMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
self.autoSyncMOC = [RKObjectManager sharedManager].managedObjectStore.persistentStoreManagedObjectContext;

[self.autoSyncMOC performBlockAndWait:^{

//do normal fetch
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:...];
fetchRequest.predicate =[NSPredicate...
NSArray *result = [self.autoSyncMOC executeFetchRequest:fetchRequest error:nil];

//do stuff with fetched objects (i.e. set a property)
for (DownloadableItem *item in result) {
item.checksumDownload = @"test";

//save context
[self.autoSyncMOC save:...];

What can go wrong here? I think I still don't understand the concepts.

I know that one needs more code and context to give a proper answer. I just want to know if my code is valid.


Answer Source

It looks like this is a bug. Unfortunately, still unresolved: