Core Data model files does not load on rename
Asked Answered
A

7

15

I have a model file thats named "Model". If I rename it to "SomeOtherName" it just does not get loaded.

initWithContentsOfURL returns nil and:

mergedModelFromBundles: [NSArray arrayWithObjects:[NSBundle mainBundle], nil]; 

...crashes with because it thinks there is nil in this array.

I am allowed to rename my model so whats wrong? I can not give you more info because I have none :P The SomeOtherName model is placed in the bundle and it should load just fine.

Thanks

Assignation answered 12/8, 2011 at 18:26 Comment(0)
K
29

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.

Koph answered 18/2, 2012 at 10:20 Comment(3)
I can't see this plist. Is it in the bundle?Logsdon
Sorry, found it. I guess another solution is to delete the app from the sim?Logsdon
This worked for me! thx: to open in terminal open -a textedit "filelocation"Painterly
D
2

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] ]; 
Dressmaker answered 12/8, 2011 at 23:26 Comment(2)
The merge looks like this. And its exception is: 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' *** First throw call stack:Assignation
If you use the first form and you get the error then the problem is that the mainbundle is not being returned for some reason. You should log the return from [NSBundle mainBundle] to confirm. Are you working on MacOS or iOS?Dressmaker
S
1

Using Xcode 7.2.1:

  1. Go to Project Navigator
  2. Select the data model file (e.g. MyProject.xcdatamodeld)
  3. Select that file's File Inspector
  4. Change the Model Version
Sparse answered 7/3, 2016 at 14:3 Comment(1)
This is the only solution which worked for me on my Xcode 8 and Swift 3. I changed the Tools Version to Xcode 7.3 instead of 8 and it started to work.Dextrose
H
1

I ran into this issue recently when I added a new attribute to my data model entity.

I had to do the following:

  1. Copy out the contents of my 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)
  2. Delete the 4 files that XCode generated for me: Entity+CoreDataClass.h, Entity+CoreDataClass.m, StockEntity+CoreDataProperties.h, & StockEntity+CoreDataProperties.m
  3. Add the new attributes to the entity in the .xcdatamodelId file
  4. Select the entity from the list and select Editor -> Create NSManagedObject Subclass...
  5. Add the calculated properties I copied out in step 1 back into the Entity+CoreDataClass.h and Entity+CoreDataClass.m files
  6. Select the Build Phases for my target and delete the Entity+CoreDataClass.m file from the Compile Sources list
  7. Add the .xcdatamoelId file to the Compile Sources list.

I was then able to build and run the project successfully.

Hetero answered 4/3, 2017 at 18:56 Comment(0)
U
1

This worked for me:

  1. Click on your YourName.xcdatamodel
  2. Right Click on it to show file inspector
  3. On the right you can see "Target Membership", there should be a checkmark.

(Check if the name in AppDelegate is the same as your xcdatamodel file. -> let container = NSPersistentContainer(name: "YourName") ......)

Ultann answered 20/9, 2017 at 12:29 Comment(0)
D
0

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];
Disrespect answered 12/8, 2011 at 19:2 Comment(2)
It looks like this. And I log to the modelURL to the console and put it into the Terminal and do a ls. It gives me SomeOtherName.mom and VersionInfo.plist ( the contents of the SomeOtherName.momd folder)Assignation
Are you sure you have that assert in place, e.g. the URL is not nil? Showing your Core Data context initialization code in the question might help.Disrespect
K
0

If you're having trouble tracking down the last few changes, use

grep -ir "<old model name>"

and

find . -regex "<old model name>*" -print 

in your base project directory.

Knotted answered 27/12, 2022 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.