在本地备份核心数据,并从备份恢复--Swift [英] Backup core data locally, and restore from backup - Swift
问题描述
我很难找到有关创建核心数据备份的任何信息。我的最终目标是允许用户创建多个备份,并从选定的备份进行还原。
I'm struggling to find any information about creating backups of core data. My ultimate goal is to allow the user to create multiple backups, and restore from a selected backup.
我找到了一个示例项目,允许您在本地或通过Objective-C中的iCloud进行备份/恢复,但没有任何内容在swift中。
I've found a sample project that allows you backup/restore locally or via iCloud in Objective-C, but nothing in swift.
有人可以帮忙吗?或者指出我正确的方向。我甚至不知道从哪里开始。
Can anyone help? Or point me in the right direction. I don't even know where to start with this one.
推荐答案
我从来不需要这样做,但如果我这样做,那就是我要做的。
I've never needed to do this but if I did this is what I'd do.
任何时候,请使用以下步骤:
At any time, use the following steps:
- 创建一个新的第二个核心数据堆栈。使用
NSPersistentContainer
或旧的(但仍支持)创建NSPersistentStoreCoordinator
的方法。 - 使用
NSPersistentStoreCoordinator
的函数migratePersistentStore(_:to:options:withType:)
来创建备份。使用UUID或时间戳使目标URL包含唯一的内容。将备份放在文档文件夹中。 - 按日期保留备份列表。你可以把它放在
UserDefaults
中,或者创建一个新的属性列表文件来保存备份信息。
- Create a new, second Core Data stack. Use either
NSPersistentContainer
or the older (but still supported) method of creating anNSPersistentStoreCoordinator
. - Use
NSPersistentStoreCoordinator
's functionmigratePersistentStore(_:to:options:withType:)
to create the backup. Make the destination URL include something unique, using a UUID or a timestamp. Put the backups in the documents folder. - Keep a list of backups by date. You could put this in
UserDefaults
or create a new property list file to save backup info.
步骤#2将从Core Data堆栈中删除原始存储 - 这就是您在步骤#1中创建第二个堆栈的原因。通过这种方式,您可以使用第二个堆栈进行备份,而不会影响您的应用正在使用的备份。
Step #2 will remove the original store from the Core Data stack-- which is why you create a second stack in step #1. This way you can use the second stack to make the backup without affecting the one your app is using.
如果您使用的是 NSPersistentContainer
,使用其 persistentStoreCoordinator
属性执行步骤#2。
If you're using NSPersistentContainer
, use its persistentStoreCoordinator
property to carry out step #2.
这有点棘手,因为您的应用可能正在使用其持久存储,但现在您想用旧版本替换它。 在从备份恢复之前,请确保您当前没有使用持久存储中的任何托管对象。取消分配 NSPersistentContainer
。卸载任何使用托管对象的UI。让你的应用程序进入一种状态,它可以做的就是从备份恢复或返回使用当前数据,但是除了备份列表之外它没有显示任何数据。
This is a little bit tricky because your app may be using its persistent store, but now you want to replace that with an older version. Before restoring from a backup, make sure you're not currently using any managed objects from the persistent store. Deallocate your NSPersistentContainer
. Unload any UI that makes use of managed objects. Get your app into a state where all it can do is either restore from a backup or go back to using the current data, but where it's not showing any data except the backup list.
现在你已经这样做了,
- 显示备份列表并让用户选择一个。
- 使用您的数据模型创建
NSPersistentStoreCoordinator
。 - 使用
replacePersistentStore(at:destinationOptions) :withPersistentStoreFrom:sourceOptions:ofType:)
将备份数据复制到普通应用程序位置的方法。起始位置是备份位置,目标是应用程序通常保存其数据的位置。 - (可选)使用
NSPersistentStoreCoordinator
's functiondestroyPersistentStore(at:ofType:options:)
删除备份。 - 加载
NSPersistentContainer
像往常一样重新加载常规应用UI。
- Display the backup list and let the user select one.
- Create an
NSPersistentStoreCoordinator
using your data model. - Use the
replacePersistentStore(at:destinationOptions:withPersistentStoreFrom:sourceOptions:ofType:)
method to copy the backup data to the normal app location. The starting location is the backup location, the destination is where the app normally saves its data. - (optional) Use
NSPersistentStoreCoordinator
's functiondestroyPersistentStore(at:ofType:options:)
to delete the backup. - Load an
NSPersistentContainer
as usual and reload the regular app UI.
不要使用与文件相关的直接API,例如 FileManager
用于任何此类操作。核心数据方法将涵盖所有与核心数据相关的文件,并做其他好事,例如避免导致数据损坏和尊重文件锁。
Don't use direct file-related APIs like FileManager
for any of this. The Core Data methods will cover all of the Core Data-related files and do other nice things like avoid causing data corruption and respecting file locks.
这篇关于在本地备份核心数据,并从备份恢复--Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!