Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIManagedDocument migrate data model

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

It was all working fine until I changed my core data model / scheme (adding a new model version - like I had several times in 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 app I don't seem to be able to load my documents with UIManagedDocument's -openWithCompletionHandler:. I can create new documents and read/write those. If I change the data model version back 1 then I am able to read the existing docs, but not the new ones.

From what I understand I am only do lightweight migrations to the data model and UIManagedDocument is supposed to handle that right?

Any advice would be greatly appreciated!

like image 293
adamteale Avatar asked Jan 18 '23 22:01

adamteale


1 Answers

Given below is based on my understanding:

NOTE - I haven't tried it for iCloud but I have tested it for non-icloud and seems ok.

UIManagedDocument configures the managedObjectModel and a Persistent Store Coordinator by itself

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

//Note - In this example, managedDocument is a UIManagedDocument property

self.managedDocument.persistentStoreOptions = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                                        [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

Refer:

  • Apple Documentation on Core Data Versioning
  • Point of using NSPersistentStoreCoordinator?
like image 144
user1046037 Avatar answered Mar 12 '23 21:03

user1046037