views:

191

answers:

2

I am trying to push an updated version of my sqlite db to my device and so far have not figured out a way to do it other than renaming it each time (painful). The stale (cached?) copy remains permanently on the device.

I understand how to get an updated version into the simulator by clearing the /applications folder, but this does not seem to affect the physical device.

Suggestions?

A: 

You could write an OS X command line app to 'diff' the old and the new db. Then send the device a JSON object (or XML) of all the adds, deletes, and updates and apply them to the existing store.

You could also copy your store to the Documents folder on the first run of the app and read it from there. The Documents folder has read/write/delete access so I think you could just sub the new one in. You can get that path like so.

NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(
      NSDocumentDirectory,
      NSUserDomainMask,
      YES);

NSString *docDir = [arrayPaths objectAtIndex:0];
Joe Cannatti
I suppose that could work, but sounds like more work than just renaming it. :)How do you handle the situation where you release a new rev of your app to the app store and you need to make schema changes?
Jason
I don't think it would work if you had to make a schema change. That has not come up for me yet. I think you could write the store to the documents folder, which is outside of the bundle and has read/write access.
Joe Cannatti
+2  A: 

If the database file is part of your app bundle when you update the app you'll update the database.

If the database is not part of the app bundle but is in a read-write space (like the documents directory) you'll need to implement your own strategy.

I include a schema_version table my database:

create table schema_version (
  version integer not null
);

and then on app start up, I check the current version and then look in my app directory for a update-schema-[N+1].sql file to apply as an update. So if my schema version is 4, I'll check for update-schema-5.sql etc. (and I repeat this as needed until I run out of update-schema-N.sql files to apply.)

Be advised that the write performance of sqlite on the iphone isn't all that great. If you need to update 200K database rows, you'll likely need to figure out some way of keeping the user amused while it is going on.

My app uses a hybrid approach where we have the majority (60 MB) of our data in a read only database and a smaller read-write database for user contributed and cached content.

Carl Coryell-Martin
Perfect! When you want to push a new version of the db to your local physical test device (iphone / ipod touch) what is the easiest approach? I have just been renaming my db each time which forces the new version to be picked up / used.
Jason
I Build:Clean ([shift-command-k]) after I update the database.
Carl Coryell-Martin