In the past I have released my app with a database that was preloaded, so the user didn't have to update it on the first run. There was some code I found in another question on SO (sorry, don't have the link anymore) that I added to my App Delegate's persistentStoreCoordinator
method:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"db.sqlite"];
if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]])
{
NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"db" ofType:@"sqlite"]];
NSError* err = nil;
if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&err])
{
NSLog (@"Error - Could not preload database.");
}
}
//... more code generated from the template here
}
When I try to do this in iOS 7, I don't get any errors, but the database is empty (even though the database in my mainBundle
has all the info I'm expecting). I noticed that there are more database files (a .sqlite-shm file and a .sqlite-wal file) in the applicationDocumentsDirectory
. Do I need to do something with those files as well? Or is it no longer possible to have a preloaded database ship with the app?
EDIT: I tried adding code to copy the new .sqlite-shm and .sqlite-wal files as well, but that doesn't help.