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开发者_Python百科 clearing the /applications folder, but this does not seem to affect the physical device.
Suggestions?
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.
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];
精彩评论