I have a CoreData database working perfectly with my application. The database stores presets, saved by the user. I want to give the user the ability to backup the database using File Sharing via iTunes.
Basically I want to:
Backup: Database -> file the user can transfer between devices
Restore: file -> Database
The most straightforward way I thought of was to simply copy and replace the persistentStore. Like so..
This is how I create the database:
self.database = [[UIManagedDocument alloc] databaseURL];
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.database.fileURL path]]) {
[self.database saveToURL:self.database.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) { }];
}
This is how I replace the persistentStore:
- (void)restoreBackupWithBackupURL:(NSURL *)backupURL {
// close the current document
[self.database closeWithCompletionHandler:^(BOOL success) {
// GET PERSISTENT STORE URL
NSURL *databaseURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
databaseURL = [databaseURL URLByAppendingPathComponent:@"Default Database"];
databaseURL = [databaseURL URLByAppendingPathComponent:@"StoreContent"];
databaseURL = [databaseURL URLByAppendingPathComponent:@"persistentStore"];
// DELETE PERSISTENT STORE
NSError *error;
BOOL deleted = [[NSFileManager defaultManager] removeItemAtURL:databaseURL error:&error];
if (!deleted) {
NSLog(@"Error deleting file: %@", error.localizedDescription);
return;
}
// COPY BACKUP PERSISTENT STORE
BOOL copied = [[NSFileManager defaultManager] copyItemAtURL:backupURL toURL:databaseURL error:&error];
if (!copied) {
NSLog(@"Error copying file: %@", error.localizedDescription);
return;
}
// INITIALIZE
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"Default Database"];
self.database = [[UIManagedDocument alloc] initWithFileURL:url];
}];
}
I have it working, however, I am unsure if it is a safe and preferred solution (new to core-data). Any input?