I have a Mac app using Core Data with SQLite. In this case, the database is in 3 files, the storedata file, the .shm file, and the .wal file. I want to back these up from the app to a location of the user's choosing. My code asks the user for the location, then creates URL's representing the sources and destinations. I check for the file's existence and remove if necessary before copying.
NSError* error;
if ([[NSFileManager defaultManager] fileExistsAtPath:backupDBURL.path]) {
/
NSError* error;
[[NSFileManager defaultManager] removeItemAtURL:backupDBURL error:&error];
if (error) {
NSLog(@"error: %@", error);
}
}
if ([[NSFileManager defaultManager] fileExistsAtPath:backupShmURL.path]) {
/
NSError* error;
[[NSFileManager defaultManager] removeItemAtURL:backupShmURL error:&error];
if (error) {
NSLog(@"error: %@", error);
}
}
if ([[NSFileManager defaultManager] fileExistsAtPath:backupWalURL.path]) {
/
NSError* error;
[[NSFileManager defaultManager] removeItemAtURL:backupWalURL error:&error];
if (error) {
NSLog(@"error: %@", error);
}
}
Then I copy each one with:
[[NSFileManager defaultManager]copyItemAtURL:appDelegate.storeDataURL toURL:backupDBURL error:&error];
if (error) {
NSLog(@"error: %@", error);
}
[[NSFileManager defaultManager]copyItemAtURL:appDelegate.storeDataURL toURL:backupShmURL error:&error];
if (error) {
NSLog(@"error: %@", error);
}
[[NSFileManager defaultManager]copyItemAtURL:appDelegate.walURL toURL:backupWalURL error:&error];
if (error) {
NSLog(@"error: %@", error);
}
The really strange this is that the first file copies fine, which is the main database file. However, each of the others failes with a message like: "couldn’t be copied because you don’t have permission to access". I have tried copying to many locations including the desktop, the documents folder, and others. Same error.
Here's the full error message attempting to copy the second file to my user folder:
2015-07-26 09:56:13.425 Mac Inventory[54658:7880735] error: Error Domain=NSCocoaErrorDomain Code=513 "“Inventory.storedata” couldn’t be copied because you don’t have permission to access “myNameHere”." UserInfo=0x6080000f5100 {NSSourceFilePathErrorKey=/Users/rickschlueter/Library/Containers/com.rschluet.MacInventory/Data/Library/Application Support/Inventory.storedata, NSUserStringVariant=(
Copy
), NSDestinationFilePath=/Users/myNameHere/Inventory.storedata-shm, NSFilePath=/Users/myNameHere/Library/Containers/com.rschluet.appNameHere/Data/Library/Application Support/Inventory.storedata, NSUnderlyingError=0x608000257580 "The operation couldn’t be completed. Operation not permitted"}
It's probably not the underlying reason, but you're using that API incorrectly. You should be checking the return value of the method, and only looking at the error if the method returned NO.
Sorry I don't know how the OS X sandbox works so I can't suggest anything for the actual problem. Do you have some antivirus or something running that would prevent writing the file types? Are you positive you're constructing the URLs correctly?