How to move SQLite DB in swift between projects? - ios

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.

Related

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.

Issue in viewing sqlite file created by Core Data in iOS 7

I have an issue when am trying to access the database file that is created by core data. The DB file is in Documents folder. I've copied this to my desktop and opened it using Firefox plug in. Hello.sqlite file doesn't contain any rows that I have inserted values using core data. Am running app using iOS 7 simulator. I can see the rows are inserted when am using iOS-6. Do any body have an idea, why I cant see values in my Hello.sqlite file????
To view your project data do the following:
Run your xcode project on your Mac (Must have a database included)
Launch CoreDataPro
Select Menu->File->New Project
Select the "File" tab you could use file tab and browse to "/Users/UserName/Library/Application Support/iPhone Simulator/6.0/Applications/CD000457-0F95-4AFF-9C0A-829F681557AF(select like wise folder)"/your application" copy and paste on Desktop
Step 2: Persistence file load that is your .sql File
copy .sqlite file from where you can store (i.e /Users/Your User Name/Library/Application Support/iPhone Simulator/6.0/Applications/CD000457-0F95-4AFF-9C0A-829F681557AF/Documents)
Select Open
Browse your Application data
Download from Wiki
For More detail Visit here
Direct Coredata Download Link
This is because of a new SQLite journal mode used in iOS 7. If you set it to the old mode as shown in the answer to the following SO question you should see your data again.
How to disable WAL journal mode
I hope the below link is useful for u
http://www.codigator.com/tutorials/ios-core-data-tutorial-with-example/

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.

How to make core data of Ios has the ability to read sqlite which is generated from other platform?

I generate some sqlite database which using Ruby or Php, and I want to use them both for my Ios Apps and Android Apps.However,everything for Android got fine,but not Ios.
The App of Ios would crash.So I wonder if there is someway to make the core data can read the sqlite database which generate from other platform
Only workaround is..
Generage an sqlite database file using core data. Dump the data into this file manually and keep it in the app bundle. Once the application launch first time, replace the existing sqlite database file in the documents folder with the one in the bundle. Automatically the core data will use populated sqlite database.
Follow the steps:
Create a core data sample project using Xcode. Add the data model file and create the entities.
Run the project. Coredata will create empty sqlite file in the documents folder.
Take the sqlite file and manually populate the tables using what ever easy way you have.
Add the populated sqlite file to the app bundle. You can't edit this file at runtime so you have to place the same in the documents folder at first launch (replace the existing file).
Coredata will take your sqlite file hence forth. So that you can also edit the sqlite file.

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.

Resources