Migrating a UIManagedDocument Data Model

I am working on an iPhone application that uses a subclass of UIManagedDocument and stores its documents in iCloud.

Everything worked fine until I changed my main model / data scheme (adding a new version of the model - as it has been several times over the past few weeks). I added a new property and changed the data type of one of the existing properties.

Now, when I run my application, I seem to be unable to load my documents using the UIManagedDocument -openWithCompletionHandler: I can create new documents and read and write them. If I change the version of the data model back to 1, then I can read existing documents, but not new ones.

From what I understand, I only do light migrations to the data model, and should UIManagedDocument handle this right?

Any advice would be greatly appreciated!

+4
source share
2 answers

In a subclass of UIManagedDocument, you can try to override managedObjectModel as follows:

 - (NSManagedObjectModel *)managedObjectModel { NSString *path = [[NSBundle mainBundle] pathForResource:@"<ModelNameHere>" ofType:@"momd"]; NSURL *momURL = [NSURL fileURLWithPath:path]; NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL]; return managedObjectModel; } 
+2
source

Below is my understanding:

NOTE. I have not tried it for iCloud, but I tested it for non-icloud and it seems to be fine.

UIManagedDocument independently configures managedObjectModel and persistent storage coordinator

When the migration needs to be done, just set the UIManagedDocument persistentStoreOptions

 //Note - In this example, managedDocument is a UIManagedDocument property self.managedDocument.persistentStoreOptions = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

Cm:

+6
source

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


All Articles