How do we prevent "CoreData cannot execute error"?

We get "CoreData cannot complete the error" every once in a while. We read the Apple documentation, but it is unclear what can be saved. We are very careful about creating one context for each thread, etc. However, one thing our application does is that we store NSManagedObjects on our UIViewControllers (usually via NSArray or NSDictionary). I guess what happens because the relationships between the objects are changing, and we are not processing the corresponding notification.

Does anyone have any suggestions for a better design regarding Core Data? When we get an error, I don’t see that we actually removed anything from the context to cause the error. Do I need to handle NSManagedObjectContextObjectsDidChangeNotification on our UIViewControllers if they maintain state? We appreciate any suggestions.

+6
source share
1 answer

You can register for notifications of changes to Core Data. This will allow you to update managed objects as they change. For more information, see Master Data Documents. You will be interested in 2 methods of registration and response to changes:

[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeChanges:) name:NSManagedObjectContextDidSaveNotification object:(your NSManagedObjectContext)]; 

The mergeChanges selector (your method) will call the following method to synchronize any changes from other threads. It will look something like this:

 - (void)mergeChanges:(NSNotification *)notification{ AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appDelegate managedObjectContext]; // Merge changes into the default context on the main thread [context performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) withObject:notification waitUntilDone:YES]; } 
+5
source

Source: https://habr.com/ru/post/894453/


All Articles