Core Data - Failed to load optimized model at path

后端 未结 5 1630
南方客
南方客 2020-11-27 15:21

I\'m getting some of these prints in my console while running my application from Xcode 6 in my iPhone 6 with iOS 9 beta 5:

CoreData: Failed to load o

5条回答
  •  萌比男神i
    2020-11-27 16:15

    I want to answer to people who ran into this when was writing own pod which has own CoreData models. Probably, you've put your model definition to the bundle (that's good), but you search for the momd file in the wrong bundle.

    Let's say you have defined your bundle in podspec like this:

    'MYPodBundle' => [
        'Model/*.{xcdatamodeld,xcdatamodel}'
    ]
    

    Then you should first find this bundle, and then locate your model inside it.

    NSURL *bundleURL = [[NSBundle bundleForClass:[MYEntity class]] URLForResource:@"MYPodBundle" withExtension:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
    NSString *modelPath = [bundle pathForResource:@"MYCoreDataModel" ofType:@"momd"];
    NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];
    

    So you can continue creating CoreData stack.

    //This may be a bit offtopic because you weren't writing own pod, but your answer is on google's top.

提交回复
热议问题