Rename xcdatamodel file
Asked Answered
F

10

22

What do in need to do to rename the .xcdatamodel file. Renaming .xcdatamodel filename, along with changing ...

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"newfilename" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
return __managedObjectModel;

and

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"newfilename.sqlite"];

doesn't work! I get following error:

2011-04-11 17:19:54.164 ProjectName[5826:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00fa15a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x010f5313 objc_exception_throw + 44
    2   CoreData                            0x00013853 -[NSPersistentStoreCoordinator initWithManagedObjectModel:] + 419
    3   ProjectName                         0x00003471 -[ProjectNameAppDelegate persistentStoreCoordinator] + 257
    4   ProjectName                         0x000031a4 -[ProjectNameAppDelegate managedObjectContext] + 100
    5   ProjectName                         0x000028ca -[ProjectNameAppDelegate makeSplitViewController] + 762
    6   ProjectName                         0x00002d44 -[ProjectNameAppDelegate application:didFinishLaunchingWithOptions:] + 68
    7   UIKit                               0x001f3c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
    8   UIKit                               0x001f5d88 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439
    9   UIKit                               0x00200617 -[UIApplication handleEvent:withNewEvent:] + 1533
    10  UIKit                               0x001f8abf -[UIApplication sendEvent:] + 71
    11  UIKit                               0x001fdf2e _UIApplicationHandleEvent + 7576
    12  GraphicsServices                    0x018f9992 PurpleEventCallback + 1550
    13  CoreFoundation                      0x00f82944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    14  CoreFoundation                      0x00ee2cf7 __CFRunLoopDoSource1 + 215
    15  CoreFoundation                      0x00edff83 __CFRunLoopRun + 979
    16  CoreFoundation                      0x00edf840 CFRunLoopRunSpecific + 208
    17  CoreFoundation                      0x00edf761 CFRunLoopRunInMode + 97
    18  UIKit                               0x001f57d2 -[UIApplication _run] + 623
    19  UIKit                               0x00201c93 UIApplicationMain + 1160
    20  ProjectName                         0x00002599 main + 121
    21  ProjectName                         0x00002515 start + 53
)
terminate called after throwing an instance of 'NSException'

Note: I'm using Xcode4.

Feature answered 11/4, 2011 at 12:22 Comment(1)
I guess more details would bring answers, like an exception message/trace.Strobila
F
25

I have been having the same problem with Xcode 4. Don't know what the behaviour is in Xcode 3.

Firstly, when you add a core data model in xcode, you actually get a .xcdatamodeld file which is a package like the .xcodeproj file. In finder, you can right click it and "Show Package Contents". You'll see it contains a .xcdatamodel file.

It seems to me you can't rename it fully using xcode. Xcode will change the name of the package file (the momd) as you see it in the UI, but on the file system it has not actually changed.

The nil model error you get is because it can't find the newfilename momd since it still has the old name on disk. (I guess)

I worked around this by creating a new model with the name I wanted, then in the existing model, you can click-drag and select your entities, then just paste them into the new model file. You can then delete the old one and commit all the changes to your SCM.

Faultless answered 3/5, 2011 at 16:41 Comment(2)
This fixed it for me - recreating and deleting.Doris
Interesting issue with Xcode 12...due to UI changes, I didn't click and drag my entities, I copied and pasted them into the new model file. In the new file, many of my inverse relationships, delete rules and all of my codegen settings were lost. Once I fixed those problems, everything was fine.Trent
C
32

There is a hidden file called .xccurrentversion which should be in the same location as your whatever.xcdatamodeld file. Inside there is a reference to your model which needs to be updated with the new name.

In some cases, you'll have to remove the .xcdatamodeld file from the project and add it again to make it work.

Cacilia answered 12/3, 2012 at 10:41 Comment(5)
+1 for full understanding of problem. Renamed an .xcdatamodeld file and this problem appeared. Why can't Xcode automatically change this??Essayist
worked form me.. the file mentioned is a hidden file. Here are the steps to show hidden files in MAC OS X Lion joshklein.net/how-to-show-hidden-files-in-mac-os-x-lionFlowage
Brilliant ! My app was continually crashing since trying to (carefully) rename the project... removing & re-adding the .xcdatamodelId file fixed the issue. Thanks!Liana
Wow, that is pretty appalling. Really does seem the only way to make Xcode change the layout/naming with a .xcdatamodeld structure is to do it yourself on disk, then re-add to XcodeOpponent
still relevant after 10 full years!Nieberg
F
25

I have been having the same problem with Xcode 4. Don't know what the behaviour is in Xcode 3.

Firstly, when you add a core data model in xcode, you actually get a .xcdatamodeld file which is a package like the .xcodeproj file. In finder, you can right click it and "Show Package Contents". You'll see it contains a .xcdatamodel file.

It seems to me you can't rename it fully using xcode. Xcode will change the name of the package file (the momd) as you see it in the UI, but on the file system it has not actually changed.

The nil model error you get is because it can't find the newfilename momd since it still has the old name on disk. (I guess)

I worked around this by creating a new model with the name I wanted, then in the existing model, you can click-drag and select your entities, then just paste them into the new model file. You can then delete the old one and commit all the changes to your SCM.

Faultless answered 3/5, 2011 at 16:41 Comment(2)
This fixed it for me - recreating and deleting.Doris
Interesting issue with Xcode 12...due to UI changes, I didn't click and drag my entities, I copied and pasted them into the new model file. In the new file, many of my inverse relationships, delete rules and all of my codegen settings were lost. Once I fixed those problems, everything was fine.Trent
O
9

Thanks to all of you for helping me finally nail this one down. Here's how I fixed this after renaming my project in Xcode 4.2:

1) Delete the reference to the *.xcdatamodeld file in your project. If it's like mine, an editor won't load when you select it. This tells me a link was broken in the rename.

2) Right-click on the .xcdoeproj file for your project and select 'Show Package Contents'.

3) Drag the *.xcdatamodeld file you find there back into your project. Done.

My app buil

Office answered 11/10, 2011 at 19:18 Comment(0)
F
3

In XCode 4.3.2, it successfully renames the model file but I had to remove it from the project and add it again to make it work. Otherwise it was throwing

Frenzy answered 14/5, 2012 at 18:44 Comment(1)
Copying the file in finder and then deleting in xcode aswell as deleting the file not just the reference worked for me and re adding worked for meWellfed
C
2

This can be done in the following manner: open .xcdatamodeld in XCode. In the inspector panel go to the Identity and Type section. In the Full Path field click on the arrow icon next to the full path. In Finder duplicate the xcdatamodel that you wish to rename. Rename the duplicate to the desired name. Back in Xcode, click on the directory icon next to the Location field. Select the xcdatamodel duplicate that you renamed to the desired name and click Choose. You can delete the old original xcdatamodel if you wish.

Cl answered 16/3, 2020 at 20:4 Comment(0)
S
1

Looks like momd missing in resources files. You can check in generated .app.

Make sure it is in compile sources build phase.

And clean/rebuild sometimes helps with xcdatamodels... Especially when creating/renaming them...

Strobila answered 11/4, 2011 at 12:38 Comment(2)
That's the thing. The momd file is not renamed. How can i fix this?Feature
try a "hard clean" by deleting all build/ content. Check the build log in details to see what's generated (and why). Also take care of versioned models .xcdatamodel_d_ vs .xcdatamodelonly models (without d) will end in resources.Strobila
A
1

In my case I had a file called Model.xcdatamodeld in my project, I decided renaming it to MyModel.xcdatamodeld through XCode. It did correctly, however the inner file was still called Model.xcdatamodel. I could not rename it through XCode, so I did through my friend Finder. The problem from now on was that Xcode wasn't opening this file anymore. Then what I did was just searching for it on the project.pbxproj and renamed the file name. Now Xcode could open it perfectly.

1D707FAE1FA9EBAB00A6D123 /* Model.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = MyModel.xcdatamodel; sourceTree = "<group>"; };

Of course that I also had to rename it on the AppDelegate.

Aun answered 1/11, 2017 at 15:51 Comment(0)
D
1

XCode 10 Update & Swift 4

Things may have changed a bit since XCode 4.2 It seems simplier in later versions because I just had to :

  1. Change OLD.xcdatamodeld to NEW.xcdatamodeld in fileInspector left tab

  2. In AppDelegate juste change the reference name : let container = NSPersistentContainer(name: "NEW")

Dangle answered 13/11, 2018 at 14:29 Comment(1)
No they haven't, I'm using Xcode 11.4 and the problem is still the same. Renaming the xcdatamodeld won't rename the xcdatamodel file in it.Stomodaeum
F
0

You can't change the name of any file in the application bundle after the app is built. Once the app bundle is fixed, it can't be changed at all.

So, if you want to change the name of the model file on device, especially on user's devices, well, you can't.

However, if you are still in development just change the name of the file in Xcode by clicking on the file's name and changing it.

Fop answered 11/4, 2011 at 21:2 Comment(0)
G
0

In Xcode 4.2, I renamed my model (*.xcdatamodeld) file in project navigator (it automatically renamed *.xcdatamodel file inside a package, and both files in my filesystem).

In initWithContentsOfURL: of the NSManagedObjectModel, I used a new name.

This resulted in what @Mustafa is describing in the question (nil model).

So I tried adding a new version of my model and it solved the problem (even though I had no changes in my model).

Gig answered 10/10, 2011 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.