Josh
Josh

Reputation: 225

How to preload data into an iOS app using sqlite files

Prior to the new Xcode 5 build, I was able to preload data into my core data powered app by referencing the file from the project. However, ever since I upgraded my XCode, my json data is now being saved into 3 files:

  1. an empty .sqlite file

  2. a .sqlite-wal file

  3. a .sqlite-shm file.

How do I do import this data into my app?

In the past all I had to do was copy the sqlite file and I was done. Now, I have no idea what file I'm supposed to copy.

Upvotes: 3

Views: 2111

Answers (5)

Sean Rice
Sean Rice

Reputation: 39

I can confirm "compacting" the database using the FIREFOX add-on provided by @Lukasz Kowalski herein is confirmed with 10.9.1 / Xcode 5 / iOS 7. To clarify procedure... download Firefox, then get his SQLite Manager add-on.

  1. Copy enclosing StoreContent folder with PersistentStore, PersistentStore-wal and PersistentStore-shm files to desktop for re-combination into single pre-iOS7 UIManagedDocument database that functions as before. where to find:

    ~/Library/ApplicationSupport/iPhoneSimulator/7.xx/Applications/gobble-d-gook/APPNAME/Documents/APPNAMEDatabase/StoreContent/{your database files}
    
  2. Launch Firefox, open window, from menu Tools > Sqlite Manager

  3. Use SQLite Manager to open database: desktop > StoreContent > PersistentStore
  4. Select from Menu > Compact Database option.
  5. Select from Menu > Close Database
  6. done! they are combined.

Upvotes: 2

Lukasz Kowalski
Lukasz Kowalski

Reputation: 101

I have found solution to create 1 .sqlite file when you have 3 files: .sqlite, .sqlite-wal and .sqlite-shm. To do that you should use „SQLite Manager” addon for Firefox (https://addons.mozilla.org/pl/firefox/addon/sqlite-manager/?src=userprofile):

  1. install and run addon in Firefox
  2. open your coredata .sqlite from directory of your app in iPhone Simulator
  3. use Compact Database which will produce 1 .sqlite file :)
  4. now you can use old method (prior to XCode 5) to include .sqlite into your application

Upvotes: 0

Josh
Josh

Reputation: 225

I found out from apple's developer site that in oder to get core data to behave in the old way (pre-iOS 7), and generate just the sqlite file, I had to add this to the persistentStore addStore method, under the options parameter.

 @{ NSSQLitePragmasOption : @{ @"journal_mode" : @"DELETE" } }

I hope this helps anyone else who encounters this problem.

Cheers

Upvotes: 1

Joride
Joride

Reputation: 3763

You could also save the JSON in a txt file and on load parse it into Core Data. Depending on how much data you have, you should do this in batches with the first batch being just large enough to populate the first view and then continue parsing the rest. All in a background queue off course. Not as easy as it was before, but this is a solution. Maybe someone else has a better solution?

Upvotes: 0

Joride
Joride

Reputation: 3763

Try just the sqlite file. See if it works, add the other two one by one.

Upvotes: 0

Related Questions