Reputation: 199
I'm writing an app that requires a writable sqlite file to be in the app's documents directory. I have the following code which is called in my AppDelegate method didFinishLaunchingWithOptions
method.
- (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:@"photos3.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
{
NSLog(@"database exists");
return;
}
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"photos3.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
The file name and extension are correct; and have worked with previous versions of the database. The database is located within my project, but when I run the code, I get the following message:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to create writable database file with message 'The operation couldn’t be completed. (Cocoa error 260.)'.'
Googling this error code tells me that it is a "read error (no such file)." However, the file is clearly there in the project. Can anyone help me solve this problem?
Upvotes: 2
Views: 827
Reputation: 199
Fixed by going to Project > Build Phases > Copy Bundle Resources and adding the sqlite file there. I thought it had already been added, but was mistaken.
Upvotes: 3