I have a copy of my db in Supporting Files folder. I need update a couple of tables and I am trying to use method to make writeable copy of db.
- (void) createEditableDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *writableDB = [documentsDir stringByAppendingPathComponent:@"yc_ch.db"];
success = [fileManager fileExistsAtPath:writableDB];
NSString *defaultPath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"yc_ch.db"];
success = [fileManager copyItemAtPath:defaultPath toPath:writableDB error:&error];
if (!success)
{
NSAssert1(0, @"Failed to create writable database file:'%@'.", [error localizedDescription]);
}
}
I have created (typed, physically) the ‘Documents’ folder in the root. At moment hierarchy of docs looks like this:
When I run my app. I am getting NSAssert1(0, @”Failed to create writable database file:’%@’.”, [error localizedDescription]);.
What’s wrong with it?
You copy the file even if the file already exists in the Document, I think NSFileManager copyFileAtPath: will return NO if file already exists at destination, hence failing your check and raising your NSAssert
So the file isn’t copied over if it already exists in your document. If that’s what causing the error, can you actually post the assertion, [error localizedDescription] will at least tell you what is wrong with it at the moment.
Edit: