CoreData - copy model to Documents folder? - ios

I have a project that uses Core Data.
When the app runs for the first time, do I have to copy the model to the Documents folder, so that the user can make changes?
Thanks,
RL

possibly.
if you are starting out with a pre-existing dataset, you can include the default dataset in the app bundle, and copy it to the document directory, where it will be writable.
however if it is starting as an empty store, it will automatically be created in the right place in the current Xcode templates.

Related

Where to paste sqlite DB into iOS project

I've just been handed over an app with pretty vague instructions of how to update an internal sqlite DB.
In the past, I've done this with Cordova by just replacing an existing sqlite db into /www/ dir.
I also created a SwiftUI project using FMDB. I believe the code itself would automatically create a non-existing DB in app's Documents Directory. My code to modify the DB would then just modify this same DB in that same location. I never formally replaced the DB itself.
Anyways, instructions given to me for this app are to simply replace the DB (just a copy and paste of the .sqlite file) into the Project's root directory. This is literally just using Finder and copying and pasting. However, I don't seem to be getting the new data.
Should I be doing this in Xcode? I.e. 'Adding new file by right-clicking. Should I then copy file to project and/or add to target?'
I'm not sure this 'copy from Finder to project dir' is actually right. Quick glance at code shows that it is attempting to read DB from app's documents directory.
Is there a way to actually copy the DB to the app's documents directory?
Note: I noticed while building the app that it says something like "Copying db.sqlite to app". So that indicates to me that perhaps there's some kind of build rule that copies from the project Dir to the app's Documents Directory.
Any insights would be appreciated.
Thanks!
You need to add your DB to your project's structure by dragging it or File->AddFiles with selecting copy to project target
After that in your code you can read it like this
let db = Bundle.main.url(forResource: "File", withExtension: "sqlite")
And since you can't do any update operations to main bundle then you should copy this file to your document's folder the first time app launches , then read it from there any upcoming launch

How to move SQLite DB in swift between projects?

I have created a SQLite DB in a project and populated it (in the simulator) with information that I need in another project as well.
Now, I have created another project in Xcode and I want to use my existing DB in that project as well (with the info that is already in the DB of course).
Is this possible? And how?
Short desc.: DB is created in an app in simulator and filled with data in that app.
I want to use the same DB with filled data in a new app that I am programming.
All you need to do is copy the generated database file from the simulator's sandbox to your new project.
To find the file, add a print statement to your project that prints the path to the database file. Then you'll where it is on your computer.

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.

Download .xcdatamodeld file and create .momd programmatically

I'm toying a lot with core data lately, and what bugs me is that you create a .xcdatamodeld to create your base structure, but then xcode convert it in a .momd file
My question is this : is it possible to do the conversion programmatically, instead of having it done by xcode?
Let's say for instance we wan't to be able to download a xcdatamodeld file from internet in an iOS app, and have it interpret it and create the underlying base.
You can create a momd file starting from an xcdatamodeld source using the momc script from Apple.
An xcdatamodeld “source” directory is compiled into a momd deployment directory, and an xcdatamodel “source” file is compiled into a mom deployment file.
(As explained here https://developer.apple.com/library/mac/documentation/cocoa/conceptual/coredata/articles/cdUsingMOM.html)
How to use the script:
Make a copy of the xcdatamodeld file you want to transform. It will be replaced.
Open the terminal and go to the Developer bin folder
cd /Applications/Xcode.app/Contents/Developer/usr/bin/
Run the momc script on your xcdatamodeld file
Usage: momc source destination
Example (it's important to put extensions):
./momc input.xcdatamodeld result.momd
If you download the entire xcdatamodeld bundle (it's a directory) and unpack it, you can use code from my momcom project to compile it into a .momd. You could then load that like any other data model. This project is still somewhat experimental, but it's performed well in testing so far. [Update: the project won't work on iOS as it is, because it uses NSXMLDocument. If you wanted to try this, you could use TouchXML, which is designed to be a drop-in replacement for NSXMLDocument. I don't know of any other options for compiling a Core Data model in your own app.]
The usual rules about having a data model and a persistent store match up apply. You couldn't just take the newly compiled model and use it with an existing store unless you handled model migration. Also, I have no idea what Apple's attitude would be regarding an app that downloaded new data model files-- it might be fine, or it might get you into some trouble with them.
No you can't download a xcdatamodeld file from internet and put it working. If you do so your programme will crash because of wrong data model. your data model needs to be in the main bundle and you have to select current data model. Core data maintain model version and some information about your data model. Even if you change your model and do not migrate the model your programme will crash and you have to delete the application from your device/simulator.
However, you can download the .sqlite database but data model should be the same.

Deleting core data version xcdatamodel files

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

Resources