我想为我的应用程序用户提供创建核心数据数据库备份的可能性,尤其是在他切换到新设备等情况下。

我该怎么做?特别是如何重新导入该文件?我的意思是,假设他制作了数据库的备份副本,然后更改了很多内容并想重置为先前保存的备份副本。我该怎么做?

谢谢!

最佳答案

让我们看一下这个示例应用程序,它包含用于进行备份,在iCloud之间复制备份以及从iCloud复制备份,通过电子邮件发送备份以及从电子邮件导入备份的功能。
http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/

顺便说一句,如果您要与ICloud之间进行备份,则使用migrationPersistentStore API进行备份/导入要安全得多。另请注意,示例应用程序假定您未使用WAL模式,这是iOS 7的默认模式。WAL模式使用多个文件,所有这些文件都需要备份或复制。

以下是视频的链接,该视频演示了示例Apps备份和还原功能。

http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/sample-apps-explanations/backup-files/

以下是用于创建备份副本的方法。请注意,可以使用多个persistentStoreCoordinators打开存储,因此在进行备份时无需将其关闭。还原它显然确实需要先删除现有存储。请注意,以下两种方法之间几乎没有什么区别,除了使用或不使用iCloud选项打开源存储。

    /*! Creates a backup of the ICloud store

     @return Returns YES of file was migrated or NO if not.
     */
    - (bool)backupICloudStore {
        FLOG(@"backupICloudStore called");


        // Lets use the existing PSC
        NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];

        // Open the store
        id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self icloudStoreURL] options:[self icloudStoreOptions] error:nil];

        if (!sourceStore) {

            FLOG(@" failed to add old store");
            migrationPSC = nil;
            return FALSE;
        } else {
            FLOG(@" Successfully added store to migrate");

            NSError *error;

            FLOG(@" About to migrate the store...");
            id migrationSuccess = [migrationPSC migratePersistentStore:sourceStore toURL:[self backupStoreURL] options:[self localStoreOptions] withType:NSSQLiteStoreType error:&error];

            if (migrationSuccess) {
                FLOG(@"store successfully backed up");
                migrationPSC = nil;
                // Now reset the backup preference
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:_makeBackupPreferenceKey];
                [[NSUserDefaults standardUserDefaults] synchronize];
                return TRUE;
            }
            else {
                FLOG(@"Failed to backup store: %@, %@", error, error.userInfo);
                migrationPSC = nil;
                return FALSE;
            }

        }
        migrationPSC = nil;
        return FALSE;
    }
    /*! Creates a backup of the Local store

     @return Returns YES of file was migrated or NO if not.
     */
    - (bool)backupLocalStore {
        FLOG(@"backupLocalStore called");


        // Lets use the existing PSC
        NSPersistentStoreCoordinator *migrationPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];

        // Open the store
        id sourceStore = [migrationPSC addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self localStoreURL] options:[self localStoreOptions] error:nil];

        if (!sourceStore) {

            FLOG(@" failed to add old store");
            migrationPSC = nil;
            return FALSE;
        } else {
            FLOG(@" Successfully added store to migrate");

            NSError *error;

            FLOG(@" About to migrate the store...");
            id migrationSuccess = [migrationPSC migratePersistentStore:sourceStore toURL:[self backupStoreURL] options:[self localStoreOptions] withType:NSSQLiteStoreType error:&error];

            if (migrationSuccess) {
                FLOG(@"store successfully backed up");
                migrationPSC = nil;
                // Now reset the backup preference
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:_makeBackupPreferenceKey];
                [[NSUserDefaults standardUserDefaults] synchronize];
                return TRUE;
            }
            else {
                FLOG(@"Failed to backup store: %@, %@", error, error.userInfo);
                migrationPSC = nil;
                return FALSE;
            }

        }
        migrationPSC = nil;
        return FALSE;
    }

/**  Sets the selected file as the current store.
 Creates a backup of the current store first.

 @param fileURL The URL for the file to use.
 */
- (BOOL)restoreFile:(NSURL *)fileURL {
    FLOG(@" called");

    // Check if we are using iCloud
    if (_isCloudEnabled) {
        FLOG(@" using iCloud store so OK to restore");
        NSURL *currentURL = [self storeURL];
        FLOG(@" currentURL is %@", currentURL);

        FLOG(@" URL to use is %@", fileURL);

        [self saveContext];

        [self backupCurrentStoreWithNoCheck];

        // Close the current store and delete it
        _persistentStoreCoordinator = nil;
        _managedObjectContext = nil;

        [self removeICloudStore];

        [self moveStoreFileToICloud:fileURL delete:NO backup:NO];


    } else {
        FLOG(@" using local store so OK to restore");
        NSURL *currentURL = [self storeURL];
        FLOG(@" currentURL is %@", currentURL);

        FLOG(@" URL to use is %@", fileURL);

        [self saveContext];

        [self backupCurrentStoreWithNoCheck];

        // Close the current store and delete it
        _persistentStoreCoordinator = nil;
        _managedObjectContext = nil;

        NSError *error = nil;
        NSFileManager *fm = [[NSFileManager alloc] init];

        // Delete the current store file
        if ([fm fileExistsAtPath:[currentURL path]]) {
            FLOG(@" target file exists");
            if (![fm removeItemAtURL:currentURL error:&error]) {
                FLOG(@" error unable to remove current store file");
                NSLog(@"Error removing item Error: %@, %@", error, error.userInfo);
                return FALSE;
            } else {
                FLOG(@" current store file removed");
            }
        }

        //
        //simply copy the file over
        BOOL copySuccess = [fm copyItemAtPath:[fileURL path]
                                       toPath:[currentURL path]
                                        error:&error];
        if (copySuccess) {
            FLOG(@" replaced current store file successfully");
            //[self postFileUpdateNotification];
        } else {
            FLOG(@"Error copying items Error: %@, %@", error, error.userInfo);
            return FALSE;
        }
    }

    // Now open the store again

    [self openPersistentStore];

    return TRUE;
}

关于iOS:如何创建核心数据库的备份副本?以及如何导出/导入该副本?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22151650/

10-14 16:25
查看更多