HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS:如何创建核心数据库的备份副本?以及如何导出/导入该副本?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想为我的应用程序的用户提供创建核心数据库备份的可能性,特别是在他切换到新设备等的情况下.

该怎么办?特别是如何重新导入文件?我的意思是让我们说他制作了数据库的备份副本,然后改变了大量的东西,并希望重置为之前保存的备份副本.我该怎么办

谢谢!

解决方法

看看这个示例应用程序,它包括进行备份,从iCloud复制备份,通过电子邮件发送备份和从电子邮件导入备份的功能.
http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/

便说一句,如果您要在ICloud之间进行备份,那么使用migratePersistentStore API来制作/导入备份会更安全.另请注意,示例应用程序假定您没有使用WAL模式,这是iOS 7的认模式.WAL模式使用多个文件,这些文件都需要备份或复制.

以下是展示示例应用备份和还原功能的视频的链接.

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

以下是用于创建备份副本的方法.请注意,可以使用多个persistentStoreCoordinator打开存储,因此在进行备份时无需关闭它.恢复它显然需要先删除现有的商店.请注意,除了使用或不使用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 @R_450_9016@eStore = [migrationPSC addPersistentStoreWithType:NSsqliteStoreType configuration:nil URL:[self icloudStoreURL] options:[self icloudStoreOptions] error:nil];

        if (!@R_450_9016@eStorE) {

            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:@R_450_9016@eStore 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 @R_450_9016@eStore = [migrationPSC addPersistentStoreWithType:NSsqliteStoreType configuration:nil URL:[self localStoreURL] options:[self localStoreOptions] error:nil];

        if (!@R_450_9016@eStorE) {

            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:@R_450_9016@eStore 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.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 %@",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.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.userInfo);
            return falSE;
        }
    }

    // Now open the store again

    [self openPersistentStore];

    return TRUE;
}

大佬总结

以上是大佬教程为你收集整理的iOS:如何创建核心数据库的备份副本?以及如何导出/导入该副本?全部内容,希望文章能够帮你解决iOS:如何创建核心数据库的备份副本?以及如何导出/导入该副本?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。