Migration Issues with UIManagedDocument

I started using CoreData in my application after the Stanford CS193P tutorials regarding using iOS 5 of the new UIManagedDocument class. The approach itself is pretty simple, but I can't figure out how to deal with the modifications to the models that I continue to make. This is how I instantiate the UIManagedDocument object (inside appDelegate, so that every other class can use it):

if (!self.database) { NSURL *url=[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; url = [url URLByAppendingPathComponent:@"AppName"]; UIManagedDocument *doc = [[UIManagedDocument alloc] initWithFileURL:url]; NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; doc.persistentStoreOptions = options; self.database=doc; [doc release]; } 

The problem is that every time I change at least a little of my .xcdatamodel, I can’t get all the contents previously saved in the document, as well as create any new instance. This actually throws the following exception:

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.' 

I thought that setting the "options" property of a managed document would solve the problem, but apparently this is not enough. Can anybody help? Could not find other questions that really fit my exact needs.

+6
source share
3 answers

Before changing your Core Data model, you should β€œAdd Model Version”.

1. Select the source model file. (e.g. YourProject.xcdatamodel)

2. "Editor" β†’ "Add model version ...". Then add a new version of the model (e.g. 2.0)

3. You will receive a new model file. (e.g. YourProject 2.0.xcdatamodel). Change it.

4. Change the current version of the model. Select the top .xcdtatmodel file β†’ View β†’ Utilities β†’ Show File Inspector. Find the "Versioned Core Data Model" tag and select the version you want to change.

It also bothered me for a long time. Hope this way helps you ^^

+2
source

There is a very simple solution to your problem. Just remove the application from the simulator or device (touch the application icon for a few seconds and touch the cross that appears when the application icons begin to vibrate). Thus, Xcode updates your UIManagedDocument application.

0
source

Make sure that you are not mistaken about NSDocumentDirectory as NSDocumentationDirectory.

  NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 

NSDocumentDirectory is right!

0
source

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


All Articles