Using Xcode (Version 8.3.3) I created a sample app with the template for Single View Application and clicking Use Core Data.
At this point my app is running and I have a database containing data that I do not want to rebuild each time the app is started.
In other words I have the files:
MyApp.sqlite, MyApp.sqlite-shm and MyApp.sqlite-wal
I copied those files to the MyApp project folder.
Now here is my question:
How do I need to change the code so that the app will use the data in the database next time it is started?
Note: the only code I have related to the question is no more than the one automatically generated by Xcode.
Related
I just submitted an app to the App store and it is now available. Unfortunately I realized that my development test data was included in the SQLite database! How do I go about emptying my database for the distribution, in other words removing all rows from tables? I know how to do this for a simulation (Reset content and settings), but since the deployed version just uses the Generic iOS build that does not run in the simulator, I am not sure how to clear the database.
Figured this out myself. You have to find the location of the sql file in your project.
Find the .sql file in your project.
Go to File-> Show in Finder
Then go to that location using Terminal and use sqlite3 to set up your DB as you want it.
Working with core data, I have build the following files:
MyApp_DB.sqlite, MyApp_DB.sqlite-shm, MyApp_DB.sqlite-wal.
Those files will be included in the app bundle when shipping and they are rather big.
Once my app is published I want the database to be read only. Therefore there is no need to copy the files above outside of the bundle.
Here is my question, how should I configure core data in my app so that I have access to my database?
Things have changed with Swift 3, so the code I had working before does not function anymore.
I want my app to work with iOS 9 and iOS 10.
Hi I struck at core data migration issue. I have developed a app which is new version of an existing app in Appstore. But i don't have source code of previous version and don't know exactly that whether core data was used in old version or not. So how to update my app (which has core data) to Appstore without any crashes. Any quick solution please?
Since you mention in a comment that you want to ignore the old data, you don't need to do any kind of migration. Model migration is about updating existing data to work with a newer data model. The only danger would be if the old app used Core Data and your new app attempts to use a persistent store file with the same file name. Then your app would attempt to load the old data, since it would find that file with the correct name.
The easiest way to test this is:
Install the existing app (from the app store)
Use this app until you're sure it's saving some data
Install the new version from Xcode. Since it's an upgrade, it will overwrite the app store copy.
By then you'll know exactly what happens when upgrading. If you get a Core Data related crash, change your Core Data setup to use a different filename. Then delete the app from your iOS device and repeat the test from above.
I have this problem: I sent to a friend my xcode project, he is able to open and run without errors, but at some point the app crashes.
More precisely app manages a sqlite database that stores the scores of the game and just when he tries to enter the page that shows the scores the app crashes with a NSRangeException as the data structure that stores the scores extracted from the db is empty.
Of course in my Xcode it works perfectly so, is there anything special I need to do before exporting a project to make it work on another mac? Should I also add the db file and make him re-import into his project?
I just created a zip file with the project folder.
If anyone can help me would be greatly appreciated. Thank you
I created one project with core data that will work with unchangeable database. And I don't want to write code in this project , that will programmatically populate this database. So, I create second project with core data, add existing xcdatamodel from first project without copying(only references). There i populate my database, open it with mozilla plug-in and it successfully filled. Then I copy ,my *.sqlite file and manually replace it with old file in first project. It causes error:"The model used to open the store is incompatible with the one used to create the store". But I use for both files the same xcdatamodelid. Where my error?
Sorry for my english, I really need help.
P.S. when I open sqlite file from first project and second (with commented code of populate base) in FileMerge - second is already empty. I appreciate any advice or help.
Karoly S nicely answers the question. I have a hint that I frequently employ that may prevent this out of sync situation. Instead of two Xcode projects trying to share one model file, just create one Xcode project with two targets. Each target will use the same model file, any class definitions derived from that model, and possibly other code. My second target is a Mac OS command line program that generates the database, while my first target continues to be the iOS app that reads that database. The Mac OS target will overwrite the database file in a project subdirectory, ensuring it's up-to-date. If I make any changes to the model, Xcode knows to update both targets.
Did you change your Model Definitions in any way? The error you are seeing is because there is some difference between the model from when you created it, and again from when you are trying to reference it. Are you running on the simulator? Try to delete both of your apps to clear the data related to it, as it might be out of date. Afterwards simply rebuild both of your projects as that will update your core data database.
EDIT: To clarify a bit more, your core data model is out of sync, this is generally caused by you building and running an app, and a database being created, then redefining your object model, this can be done in a variety of ways, most likely caused by the addition of an attribute or entity. So when you are trying to load the database there are fields that the app and core data are looking for, but are not there because they did not exist when the database was created. I hope this helps.