1

アプリの実行中にアプリにバックアップ/復元システムを実装しようとしています。データベースのバックアップを作成し、それを Backups フォルダーに入れます。復元したい場合は、永続ストアを削除し、元のファイルを削除し、バックアップをコピーして再接続します。以下の私のコードは機能します。私の質問は、これを正しく行っていますか? 私がしていることにリスクはありますか?私のコードに関するフィードバックをありがとう:

- (BOOL)resetApplicationModel {

    // ----------------------
    // This method removes all traces of the Core Data store and then resets the application defaults
    // ----------------------

    NSError *_error = nil;
    NSURL *_storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Database.sqlite"];

    NSPersistentStore *_store = [persistentStoreCoordinator persistentStoreForURL:_storeURL];

    // Remove the SQL store and the file associated with it
    if ([persistentStoreCoordinator removePersistentStore:_store error:&_error]){
        [[NSFileManager defaultManager] removeItemAtPath:_storeURL.path error:&_error];
    }

    if (_error) {
        NSLog(@"Failed to remove persistent store: %@", [_error localizedDescription]);
        return NO;
    }

    [persistentStoreCoordinator release], persistentStoreCoordinator = nil;
    [managedObjectContext release], managedObjectContext = nil;

    NSString *applicationDocumentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *originalFile = [applicationDocumentDirectory stringByAppendingPathComponent:@"Database.sqlite"];

    NSString *backupDatabaseFolder = [applicationDocumentDirectory stringByAppendingPathComponent:@"Backups"];
    NSString *backupFile = [backupDatabaseFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"DatabaseBackup.sqlite"]];

    [[NSFileManager defaultManager] copyItemAtPath:backupFile toPath:originalFile error:nil];

    // Rebuild the application's managed object context
    rootViewController.managedObjectContext = nil;
    rootViewController.managedObjectContext = self.managedObjectContext;

    return YES;
}
4

0 に答える 0