UIManagedDocument migrate data model

跟風遠走 提交于 2019-12-04 11:48:32

In a subclass of UIManagedDocument you may want to try overriding managedObjectModel like so:

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

    return managedObjectModel;
}
user1046037

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!