iOS:如何创build我的核心数据库的备份副本? 以及如何导出/导入该副本?

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

我该怎么做? 特别是我怎样才能重新导入该文件? 我的意思是让我们说他做了数据库的备份副本,然后更改大量的东西,并希望重置到以前保存的备份副本。 我该怎么做?

谢谢!

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

顺便说一句,使用migratePersistentStore API来进行备份是非常安全的,如果你正在和ICloud进行备份的话。 另请注意,示例应用程序假定您没有使用WAL模式,这是iOS 7的默认模式。WAL模式使用多个文件,这些文件都需要备份或复制。

以下是展示示例应用备份和恢复function的video的链接。

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

以下是用于创build备份副本的方法。 请注意,可以使用多个persistentStoreCoordinators打开商店,因此在进行备份时无需closures它。 恢复它显然需要先移除现有的商店。 请注意,以下两种方法之间几乎没有区别,只是源存储是使用或不使用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; } 

无论您使用的是什么持久性存储(二进制,SQLite等); 它只是文件系统上的一个文件。 您可以随时制作它的副本。

如果您在iOS 7中使用SQLite,请务必复制与其关联的其他文件,因为它们是随附的日志文件。 如果你使用的是二进制,那么只会有一个文件。

如果您只是复制文件没有导入步骤,您只需将其复制以恢复它。

还有更高级的devise,比如将整个数据库导出到可移植的东西,比如JSON,但是这是一个不同的主题。

更新

那么我已经使用了标准的Xcode核心数据模板,所以根据我刚才检查的代码,我使用了SQLite。 那么如何find所有相关的文件? 或者你能告诉我一些示例代码如何复制和插入需要的文件?

您使用NSFileManager来复制文件。 您可以查看iOS模拟器应用程序中的文档目录以查看所有文件的名称。 也可以使用NSFileManager扫描文档目录,find所有以相同文件名(例如MyData.* )开头的文件,并将其复制到备份目录中。

至于示例代码,否; 一旦您查看NSFileManager的文档,只有几行代码。