How to store database from one sqlite file (which is in our bundle) to sqlite file (which is created while we using core data) while we launching application first time.
Should we copy each entity, each row in loop or Is there any other way to do this.
You can put sqlite file directly in your bundle, for example you can create sqlite file with SQLite manager in firefox. Then when start your app you can check if file exist in the document directory, if not you need to create it. This only the first time.
Related
When I try to view the Core Data objects from my app - by downloading the App Container and going to /AppData/Library/ApplicationSupport - the MyApp.sqlite file is empty. If I open it in any SQLite database browser it shows the indexes but no objects.
The objects show up when I use Core Data inside the app, and there are many objects in the .sqlite file - but when I download it and try to view outside the app, the .sqlite is empty.
Downloading the app container
Navigating to the .sqlite file
Empty .sqlite - only contains indexes and no objects
I would suggest copy all the 3 files (.sqlite, .sqlite-shm, .sqlite-wal) to your folder and open sqlite file by using DB Browser (for sqlite) or some other tool, you will get all the data for sure while browsing the table.
I have a database preloaded in SQLite: "BenedictusCoreData.sqlite".
I add it to the project in XCode.
I modify the BenedictusCoreData.xcdatamodeld to create the entities and attributes with the same name and type as the tables and columns of my sqlite preoloaded.
I run the app in the simulator without writing any additional code. I print the main bundle path and go to the sandbox of the application. And inside /Library/Application Support/ I find the three files: .sqlite .sqlite-shm .sqlite-wal
I open the .sqlite and obviously it is empty, however Xcode has created the tables and the columns with capital letters, adding "Z" to the tablenames and "Z" to the column names, appart from adding new tables "Z_METADATA" "Z_MODELCACHE", "Z_PRIMARYKEY".
At this point, I assume that it is not a good idea just to copy my sqlite from the bundle into the "Application Support" folder and rewrite the file, because the structure is totally different.
So I am lost about how to proceed in order to copy a sqlite preloaded database into CoreData the first time you open the app.
May be you can help me.
In the appDelegate you can change the default .xcdatamodel name to the one you added. Then xcode will load your preloaded data .sqlite file instead of the default one. You should search it up there's tutorials on this
I'm using coredata in my iOS app. I'm able to save and retrieve data through the app even after terminating and relaunching it. I could see .sqlite file in document directory as well. But when I open that .sqlite file using Firefox's SQLite Manager I'm not able to see any records in the tables.
Check this. viewing coredata with sqlite browser in xcode 5
I have not tried this but if you want to https://www.codefellows.org/blog/core-data-and-database-browser-for-sqlite
one copy of .sqlite file store in your local device database that contain your User data and your project sqlite file only providing structure of your database.
i.e
/Users/"Yourusername"/Library/Developer/CoreSimulator/Devices/50923155-3C1A-4071-B2A9-94D11CDD7678/data/Containers/Data/Application/856BA865-0EA8-486A-A9B1-5A5E51005966/Documents/"sqlitefile name"
My iOS app uses CoreData and some tables need to be filled with default values every first time the app is opened since CoreData device-dependent.
So I am using NSUserDefaults to check whether it is first time open. If it is first time, I fill tables (on CoreData) with the values which I have already created and formatted lines from the txt file by reading line by line and separating in a way.
And my question is, is it safe and fastest way to use txt file for such operation?
A better option would be to keep a "canned" sqlite file in your app bundle and check for the existence of the SQLite file in your documents directory. If the file does not exist, copy the canned data from the app bundle to your documents directory.
That will skip the entire parsing logic and will allow your application to launch faster.
Marcus' approach is also what I would recommend. But you can still keep using your parsing code during development in order to have a convenient way to create the seed SQLite file. When you ship your app, package the newest seed store as a bundle resource and disable / delete your parsing code.
I have an app that generates some static data by importing from a json file into a sqlitedb, When running the app the DB file has data in it and is loaded properly, I usually go to the build folder "usually under
/Library/Application Support/iPhone Simulator/7.0.3-64/Applications/
and inspect the sqlite file and verify that it does have data.
Now I copy that same folder and paste it on the desktop, and open it in the same sqlite browser, and the data is gone?? why I don't know!!
I notice that for every sqlite file there is a -shm and -wal file generated.
Why is this happening?
Ok so I tried a little bit more investigating, I have two scenarios :
1 : I put a break point right after I finished generating the SQLite file, and then go the build folder, In the build folder the DB has data in it, if I copy that file to the desktop the db loses its data.
2 : I don;t put a break point, let the app finish normally ( gracefully ) and then go to the build folder, the db file has data, and when I copy and paste it to the desktop it still retains the data.
So I assume there's something that happens when xcode exits ( or the app closes normally ) that I am missing out on when I put a breakpoint !!
Starting with iOS 7 the sqlite database is used in Journaling mode by default - which means that all changes to the database are written to "update files", not to the database directly. You can change the behaviour back to the "old" way - have a look here for a complete explanation
:
Core Data and iOS 7: Different behavior of persistent store