I saw this post which was helpful in copying a .db file I include with my app and copy it to the appropriate directory: Where would you place your SQLite database file in an iPhone app?
Does the method that does the work provide any type of failure or error handling? Like if the user thought the app was stuck b/c the transfer was taking too long and decided to force quit the app?
Method:
// Creates a writable copy of the bundled default database in the application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bookdb.sql"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
No, It doesn’t do anything if there’s an existing file with the same name.
So if the copying was interrupted for some reason in the middle, then next time it runs it’ll think the file is already there.
Probably you should make the copying with a temporary file first and then if it finishes, rename it to the right name.