I have a model file thats named \"Model\". If I rename it to \"SomeOtherName\" it just does not get loaded.
initWithContentsOfURL
returns nil and:
The most likely cause is that new name is not being included in the build target. Check the target for included files and make sure the new name is there. If not, add it. If the old file name is there remove it before adding the new one.
If it is in the build target, confirm that the new file retains the .xcdatamodel
extension. Check the built product bundle to confirm that it includes a .mom
or .momd
file with the new name.
Your mergedModelFromBundles: should look like:
NSManagedObjectModel *mom=[NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:[NSBundle mainBundle]];
... or:
NSManagedObjectModel *mom=[NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObjects:[NSBundle mainBundle],nil] ];
Using Xcode 7.2.1:
I just ran into the same problem. Here is how I solved it:
Renaming the model file alone is not enough, because it does not rename the reference to the current model version.
It turns out the model version is stored in a separate plist file. Simply open it in a text editor and change the old name to your new model file name.
File: YourNEWModelFile.xcdatamodeld/.xccurrentversion
<plist version="1.0">
<dict>
<key>_XCCurrentVersionName</key>
<string>YourModelFile.xcdatamodel</string> <-- Change this to YourNEWModelFile
</dict>
</plist>
Please note that you should only do this if you rename the model file during development. For migrating your data model to a new version, follow the Core Data docs.
This worked for me:
(Check if the name in AppDelegate is the same as your xcdatamodel file. -> let container = NSPersistentContainer(name: "YourName") ......)
I ran into this issue recently when I added a new attribute to my data model entity.
I had to do the following:
Entity+CoreDataClass.h
and Entity+CoreDataClass.m
files to a text editor, in order to save the custom properties I had created for those the class (e.g. calculated properties)Entity+CoreDataClass.h
, Entity+CoreDataClass.m
, StockEntity+CoreDataProperties.h
, & StockEntity+CoreDataProperties.m
.xcdatamodelId
fileEditor -> Create NSManagedObject Subclass...
Entity+CoreDataClass.h
and Entity+CoreDataClass.m
filesEntity+CoreDataClass.m
file from the Compile Sources
list.xcdatamoelId
file to the Compile Sources
list.I was then able to build and run the project successfully.
How does your Core Data initialization code look like? It should look like this:
NSManagedObjectModel *managedObjectModel = nil;
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"SomeOtherName" withExtension:@"mom"];
NSAssert(modelURL != nil);
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];