What do I have to do to get Core Data to automatically migrate models?

前端 未结 10 1856
感情败类
感情败类 2020-11-27 09:14

I have read the documentation about automatic /lightweight migration for Core Data models - but I am having problems implementing it.

As I understand it the applicat

10条回答
  •  遥遥无期
    2020-11-27 09:31

    To follow up on Santthosh's answer, figured I'd post the code snippet right here instead. You need to create your managedObjectModel with initWithContentsOfURL: instead of mergedModelFromBundles: otherwise you'll get error:

    Can't merge models with two different entities XXX and XXX

    If your Model file is named "Model", here's how you create the managedObjectModel:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Model" ofType:@"momd"];
    NSURL *momURL = [NSURL fileURLWithPath:path];
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL]; 
    

    Credit to this blog post.

提交回复
热议问题