Deleting core data version xcdatamodel files - ios

I've created a versioned Core Data model in my iOS app, and it has several versions created so far (several .xdatamodel files within the .xcdatamodeld bundle). Before shipping the app, I'm considering deleting these old versions so I can start with a single clean data model - and data loss isn't an issue at this stage.
Is this possible? And what would be the best way to achieve this without simply deleting all Core data files and recreating a new model.

Deleting the old models means you cannot migrate any existing data stores. I assume you're ok with that given your statement about data loss. If you want to start fresh, you can take the current xcdatamodel, copy it somewhere else (e.g. your Desktop), then delete the xcdatamodeld folder. At this point, you can drag your saved xcdatamodel back into Xcode and re-add it as a non-versioned file.

If you want to get rid of old versions of the data model
(Note: make sure you copy the Data model file and save it somewhere in case something goes wrong)
Five Step solution
Select .xcdatamodeld file in Xcode, make sure you have the latest
version is set as the current data model.
Right, Click Show in finder if you don't see all your versions right
click again and show package contents.
Delete all the version except the latest.
Jump back to Xcode and now delete the .xcdatamodeld file just remove
reference don't move to trash.
Add the file back again
Voila it's done

Related

Copy core data file model from Objective-C app to brand new Swift 3 project

I have a first version of an iOS app written in Objective-C using core data.
I am now in the process of totally re-writing the app from scratch in Swift because the Objective-C version is very buggy.
The goal is to keep the database content in the app, after updating from Objective-C to Swift version. Users should not lose data.
In the Swift project, I used the .xcdatamodel that was automatically generated and I recreated all Entities and Relationships exactly based on the Obj-c .xcdatamodel file.
Bunddle identifier is the same for both version. In the simulator, one is replacing the other when I run projects. But the database is erased each time.
I found several topics explaining lightweight or heavy migration, but always related to a data model change inside the same project.
Is it possible to copy the old .xcdatamodel file into the new project?
I tried but without any success. Maybe problems are that files are named differently and that projects are written in different languages.
Copying a model file to a new project is literally as simple as copying the file.
If you have both Xcode projects open, you can drag the file from one project into the other. Xcode will ask what you want to do-- make sure that "Copy items if needed" is checked. Click "Finish" and you're done.
Or do it in Finder. Duplicate the old file, move the duplicate into the new project's folder, and rename it if necessary. Then switch to Xcode and tell it to add the new model file ("File" menu --> "Add files...").

How can I move an existing Core Data xcdatamodeld and sqlite to new project?

I have an existing project we'll call ABC.
I have a new project we'll call XYZ.
All I'm trying to do is copy the MyDataModel.xcdatamodeld from the ABC project folder into the XYZ project folder, and copy the associated populated MyDatabase.sqlite file from the simulator on project ABC into the simulator on project XYZ.
This does NOT work.
I get the error "The model used to open the store is incompatible with the one used to create the store"
I've tried resetting simulator, deleting all apps from simulator, running CLEAN in XCode, and still doesn't work.
When I run XYZ, it WILL create a new EMPTY sqlite database, but it will NOT accept my populated one that is associated with the ABC project and which works fine in ABC.
I need to be able to use my populated one.
The error message tells the tale. You have changed the xcdatamodeld between the time you wrote MyDatabase.sqlite and now. Core Data needs the original xcdatamodeld (unchanged) to be able to read your datafile. Retrieve it from your version control system or from Time Machine and you'll be good to go, although you might have to handle model migration.

Fix Core Data Fail

I have an app in the app store that uses Core Data and I have to release an update by tomorrow. However, I am experiencing some problems with Core Data.I by mistake made changes to my model in Core Data and now my app is crashing. I tried migrating the data but the app still crashes when I updated it on my iPhone. Is there anyway to fix it?
Thank you so much for your help!
EDIT: I am trying to add the .mom file to the app but I can not get it into the Bundle:
EDIT 2: Do I delete the entirely Planner.xcdatamodeld:
You can recover the original Core Data model from the production app's bundle.
Use Finder to open the app bundle (Show Package Contents) and look for a .mom file or a .momd directory.
Copy the file or directory to some location outside the bundle.
Create a new XCode project with Core Data.
In XCode select the newly created projects Core Data model.
Go to the XCode Editor -> Import menu and select the .mom file you have just copied, if need be find the correct version in the .momd directory.
You should now have the model in XCode, save and copy the model file to you original project...
In XCode delete the incorrect model from your project (remember to save a copy first so you have a copy of the new changes)
Use the XCode Add File to... menu to add the old model to the original project
Now compile and run the app to confirm it opens the old file correctly
Now add a new model version and add the changes to this new version - make sure you select the new model version in XCode before making changes
You should revert your model to the one that you have in App Store. Then you should Add new model version of your Core Data model. If you can't revert to that model you should try to make it exactly as it was.
After you've done a new model version, you should select that one and make the changes.
If you don't use Git (which is really, really bad, by the way), you can just install your app from the App Store and use some iPhone File Manager to get the old mom file from your app. It would be in /apps/yorapp/youapp.app/ folder with .mom extension
Try iExplorer, for example, it's free.

Core Data Lightweight Migration back to the beginning

I'm trying to cleanup my mess with coreData.
History:
- I created a coreData project for iOS
- I successfully initiated a few lightweight migrations, ending up with the xcdatamodeld folder holding numerous xcdatamodel items.
- Since I was just testing, I decided I would like to remove old xcdatamodel items from the folder. I read that deleting them would be fine, and did so.
- After deleting the previous xcdatamodel items, the folder went away, leaving me with my latest xcdatamodel item
- Development continued without the need to migrate and all was happy.
Now:
Today, I tried to perform a lightweight migration on my xcdatamodel item (not a folder). I quickly noticed that when I performed Editor - Add Model Version... I did not get the dialogue box Version Name... Based on Model... I just got a create dialogue box.
I created a Model Version but it crashed stating it could not merge...
What I want:
It appears that I need to somehow convert my lone xcdatamodel item to a xcdatamodeld item... like I started with when I created the project. I need to be able to perform lightweight migrations like I did back at the beginning.
Can anyone help?

.xcdatamodeld file not showing data model structure in Xcode

I have .xcdatamodeld file in my project but it not opening up into the data model.
When I click on the file in the left sidebar in Xcode, it continues to display the contents of the previously selected file instead of displaying the data model. Likewise, when I right click on the file and choose "Open As >", I cannot select to display it as a Data Model—nothing comes up. I checked out the code from the SVN repo and even the filer owner is not able to see it in his workspace.
This is a symptom that I have seen from time to time. In at least one case, this was due Xcode having the wrong path stored in the path component of the XCVersionGroup section of the project.pbxproj file, and having an apparently unnecessary additional name component in that section. It would probably be possible to manually edit this file, but I'm always reluctant to monkey around with the project file directly, and fortunately there's an easier way.
Navigate to your .xcdatamodeld file in the Finder. The easiest way to do this is Right click/Ctrl-click on the .xcdatamodeld file and select "Show in Finder".
Drag the file from the Finder and drop it immediately above or below the current reference to the .xcdatamodeld in the Project Navigator in your Xcode project.
Click OK to accept the import, ensuring that you are adding it to the correct targets. (Which targets are correct will depend on your project.)
You will now have two references to the same file in your Project Navigator. Clicking on either of them will now bring up the desired data model structure.
Remove the duplicate reference to the model file. Click on one of the file references (doesn't appear to matter which, though it may be better to remove the old one) and press Delete.
In the resulting confirmation dialog, be sure to select "Remove Reference" only, and do NOT move the file to the trash. (The two references point to only one actual file in the file system. Moving to trash would thus trash the only copy.)
You're now left with just one reference to the data model, and it works.
One thing that appears to trigger this bug is moving the .xcdatamodeld file in the file structure on disk. Even after correctly re-associating the file with the new location (i.e., it no longer appears in red in the sidebar), the one section of the Project file mentioned above is not correctly updated, and thus the file is not treated fully correctly as an Xcode Data Model file.
I encountered the same issue in Xcode 6.3
The only solution that worked for me was to move the .xcdatamodeld file out of my 'Resources' folder and into the project root folder in the Xcode project explorer (location on the filesystem doesn't matter)
I'm still not sure why, just moving the location of the file in the project structure causes it to not show up at all when clicked.. but apparently, at least in my setup, it doesn't like living in the Resources folder..
For some dark reasons, sometimes Xcode needs the xcdatamodeld fiel to be in the root folder.
I wrote an answer here that explain how to do this, but also a workaround to have this file where ever you would like to.
Cheers!
The .xcdatamodeld file is a package. It contains .xcdatamodel packages, one for each version of the model. Inside every .xcdatamodel there is a contents file, which is an XML with entities description. To see what is inside your packages, right-click them in Finder and choose Show Package Contents option.
Perhaps you didn't add the contents file into your repository. This way you will not be able to see your model when you create a brand new working copy from it.
I was victim of this problem today on Xcode8.
I solved it simply by placing the xcdatamodeld file outside any group in the Project navigator (left pane). As soon as I move it into a group, I can't open the little guy anymore.
I had a similar problem with Xcode 7.3.1.
It turned out to be related to the way the data model packages were configured.
I discovered that there was one embedded model package nested inside another. Let's say a top level data model called FolderNodes.xcdatamodeld containing another with exactly the same name ... FolderNodes.xcdatamodeld. Inside FolderNodes.xcdatamodeld is the actual contents file that has the model info.
The solution for me was to drag the embedded package FolderNodes.xcdatamodeld ( number 2) into the Xcode project and NOT its enclosing folder.
Apple uses the package for versioning but I think the logic associated with moving the data model file somehow breaks the original package creating a redundant wrapper.
Make sure your Version Editor is not active.
Having the Version Editor open results in the xml code to be displayed instead of the Data Model. You will also not be able to choose Data Model in the context menu of the xcdatamodeld file within the Project Navigator.
Happens on XCode 8.3 as well.
For me, moving the physical file to a different folder, then deleting the original reference, then adding it back to same place and establishing the reference again does the job.

Resources