“Collection was mutated while being enumerated” on executeFetchRequest

后端 未结 2 524
星月不相逢
星月不相逢 2020-11-30 16:08

I\'m stuck on a problem for hours now and having read everything about this on stackoverflow (and apply every advices found), I\'m now officially in need for help. ;o)

相关标签:
2条回答
  • 2020-11-30 16:50

    I was working on importing of record & display of records in tableview. Faced same issue when I tried to save record on backgroundThread like below

     [self performSelectorInBackground:@selector(saveObjectContextInDataBaseWithContext:) withObject:privateQueueContext];
    

    while I already created a PrivateQueueContext. Just replace above code with below one

    [self saveObjectContextInDataBaseWithContext:privateQueueContext];
    

    Really it was my foolish work to save on background thread while I already created a privateQueueConcurrencyType for saving record.

    0 讨论(0)
  • 2020-11-30 16:51

    OK, I think I've solved my problem and I must thank this blog post from Fred McCann's :

    http://www.duckrowing.com/2010/03/11/using-core-data-on-multiple-threads/

    The problem seems to come from the fact that I instantiate my background moc on the main thread instead of the background thread. When Apple tells that each thread needs to have its own moc, you have to take it seriously : each moc must be instantiated in the thread that will be using it !

    Moving the following lines...

    // We instantiate the background moc
    
    self.backgroundMOC = [[[NSManagedObjectContext alloc] init] autorelease];
    
    [self.backgroundMOC setPersistentStoreCoordinator:[self.managedObjectContext persistentStoreCoordinator]];
    

    ...in the _importData method (just before to register the controller as observer for the notification) solves the problem.

    Thanks for your help, Peter. And thanks to Fred McCann's for its valuable blog post !

    0 讨论(0)
提交回复
热议问题