ioscore-datansmanagedobjectmodel

URL to NSManagedObjectModel is NULL


I reworked the core data model for my app, beyond the point where lightweight migration would work. So I now have two core data models in the app bundle and I need to access each individually (for regular core data setup and for a manual migration), which means [NSManagedObjectModel mergedModelFromBundles:nil] is inappropriate / wouldn't work. The problem is that I cannot get a URL to either model, which prevents me from instantiating them with [NSManagedObjectModel alloc] initWithContentsOfURL:. These are the methods I'm using, and they both return NULL:

- (NSURL *)currentModelURL{
    return [[NSBundle mainBundle] URLForResource:@"NewModel" withExtension:@"momd"];
}

- (NSURL *)oldModelURL{
    return [[NSBundle mainBundle] URLForResource:@"OldModel" withExtension:@"momd"];
}

In fact, when I try this method on other apps, even ones with a single model, it NEVER returns the model URL. This is odd, as the previous version of my app ran just fine by getting the model via the URL using the method above... not by [NSManagedObjectModel mergedModelFromBundles].

I have read this and this and tried the proposed solutions, which include ensuring the models are added to the list of resources to copy to the bundle in the Build Phase, verifying case-sensitive filenames, and fidgeting with the file extensions in the URLForResource method. I suppose I could render the completely new model as a new version of the old one and simply prevent automatic migration in the persistent store options... but my approach should work. Why is it suddenly impossible to get URLs for models in the app bundle?


Solution

  • The Method is working 100% so the file is simply not there ;)

    either you got a typo.. or the cAsE is wrong or your model isn't versioned (!)

    compile the app and look in %APPP%/Resources/. Is there the OldModel.momd FOLDER?