复制/备份永久存储 [英] Copy / Backup Persistent Store

查看:150
本文介绍了复制/备份永久存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,当我备份应用程序的核心数据文件时,我只会在应用程序运行时将.sqlite文件复制到另一个位置。但是现在启用了日记功能(wal),该功能不再起作用。我看不到NSPersistentStoreCordinator或NSManagedObjectContext写入新文件的方法。我猜我可能有2种方法:

Normally when I backed up the core data file for my app, I would just copy the .sqlite file to another location while the app was running. But now that journaling (wal) is enabled, this does not work anymore. I cannot see a way for NSPersistentStoreCordinator or NSManagedObjectContext to write a new file. I'm guessing maybe I have 2 methods:



    • 关闭持久性存储并再次打开并使用@ {@ journal_mode:@ DELETE},然后复制.sqlite文件。

还有更好的主意吗?
谢谢。

Any better ideas ? Thank you.

推荐答案

更改日志模式将消除日志文件,因此很简单。不过,我不知道我会信任您使用它-因为无法保证Core Data实际上已经刷新了对SQLite文件的所有新更改。可能还可以,但是可能有一些内存更改尚未写入Core Data。几乎可以肯定这是安全的,但偶尔有一段时间它不会正常工作的可能性很小。

Changing the journal mode will eliminate the journal files, so it's simple. I don't know that I'd trust it for your use, though-- because there's no guarantee that Core Data has actually flushed all new changes to the SQLite file. It might be OK, but there might be some in-memory changes that Core Data hasn't written out yet. This is almost certainly safe, but there's a small chance that it won't work right once in a while.

选项2会更安全,尽管工作量更大。我将使用 NSPersistentStoreCoordinator migratePersistentStore:toURL:options:withType:error:方法创建第二个持久性存储(这些文档特别提到对另存为操作很有用)。告诉Core Data为您创建副本应确保实际复制了所有必需的内容。只是不要在您的主要持久性存储协调器上执行此操作,因为在迁移后,PSC会删除对原始存储对象的引用(文件仍然存在,但是该PSC不再使用该文件)。步骤将是

Option 2 would be safer, though more work. I'd create the second persistent store using NSPersistentStoreCoordinator's migratePersistentStore:toURL:options:withType:error: method (which the docs specifically mention as being useful for "save as" operations). Telling Core Data to create the copy for you should ensure that everything necessary is actually copied. Just don't do this on your main persistent store coordinator, because after migration, the PSC drops the reference to the original store object (the file's still there, but it's no longer used by that PSC). The steps would be


  1. 创建新的仅迁移 NSPersistentStoreCoordinator 并添加原始持久存储文件。

  2. 使用此新的PSC迁移到新的文件URL。

  3. 将所有引用都放弃对此新的PSC,请勿使用

  1. Create a new migrate-only NSPersistentStoreCoordinator and add your original persistent store file.
  2. Use this new PSC to migrate to a new file URL.
  3. Drop all reference to this new PSC, don't use it for anything else.

这篇关于复制/备份永久存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆