PersistentStore方法在本地和iCloud中创建可共

PersistentStore方法在本地和iCloud中创建可共

本文介绍了如何使用当前的NSPersistentStore方法在本地和iCloud中创建可共享的Core Data .sqlite备份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花了很长时间弄清楚创建由Core-Data支持的.sqlite文件的备份并将该备份存储在本地和/或iCloud(无论用户喜欢什么)中以供下载的正确方法. ,还原或共享.让我先说一下,我不是在谈论将持久性存储移至iCloud,以供应用程序用作其数据源.我只是问这个问题中关于创建备份文件的问题.

I have been having quite a time of figuring out the correct way to create a backup of a Core-Data backed .sqlite file and storing that backup, both locally and/or in iCloud (whatever the user prefers), for download, restore or sharing. Let me state up front that I am not talking about moving the persistent store to iCloud to be used by the app as its datasource. I am simply asking about creating backup files in this question.

2014年,Apple将Core Data SQLite存储的默认日记记录模式更改为WAL.

In 2014, Apple changed their default journaling mode for Core Data SQLite stores to WAL.

https://developer.apple.com/library/content/qa/qa1809/_index.html

有了这一更改,他们建议:

With that change, they recommended:

请注意,这是我们建议的选项.

Note that this is the option we recommend.

在此之前,我一直在使用NSFileManager创建备份.有了这个建议,我相信在本地创建备份的正确方法是添加一个新的持久性存储,然后将该持久性存储迁移到所需的备份位置,方法是使用NSPersistentStoreCoordinator方法分别为addPersistentStoreWithType:configuration:URL:options:errormigratePersistentStore:toURL:options:withType:error.

Prior to this, I had been using NSFileManager to create backups. With this recommendation, I believe that the correct way to create a backup locally is to add a new persistent store and then to migrate that persistent store to the desired backup location, usingthe NSPersistentStoreCoordinator methods addPersistentStoreWithType:configuration:URL:options:error, and migratePersistentStore:toURL:options:withType:error, respectively.

我的问题有两个:

  1. 我以前将数据压缩到NSData,然后再导出,并且将直接将NSData写入文件.我的文件扩展名是对我的应用程序自定义,以便通过电子邮件或其他方式共享压缩数据iOS共享方法.随着migratePersistentStore:toURL:options:withType:error方法,我现在最终以.sqlite文件(及其对应的WAL文件等)结尾所需的位置,但我不知道如何共享此文件现在.如果我压缩文件,就不会有丢失数据的危险.努力通过使用来保存migratePersistentStore:toURL:options:withType:error在第一个地方?我相信是这样,但我确实希望使我的用户共享/保存他们的备份可以查看电子邮件等,现在我不知道如何最好地解决此问题.
  2. 我很难理解migratePersistentStore:toURL:options:withType:error可以用来将文件备份到iCloud.就像上面的共享示例一样,我看到我可以用addPersistentStoreWithType:configuration:URL:options:error,以及migratePersistentStore:toURL:options:withType:error获取所需的.sqlite本地复制,但是如果我尝试将其本地上传文件到iCloud,我担心我会丢失我保存的数据在中使用migratePersistentStore:toURL:options:withType:error第一名.我一直在尝试看看我是否有办法可以/应该使用migratePersistentStore:toURL:options:withType:error移动新创建的persistentStore直接存储到iCloud,但是我还没有去过能够找到有关如何执行此操作或是否应该执行此操作的任何文档完全完成.是否需要使用一个特定的URL来指示persistentStore的目标是iCloud?
  1. I previously would zip my data to NSData before exporting it, andwould write the NSData directly to a file. My file extension wouldbe custom to my app, for sharing the zipped data via email or otheriOS sharing methods. With themigratePersistentStore:toURL:options:withType:error method, I nowend up with a .sqlite file (and its corresponding WAL file, etc) inthe desired location, but I cannot figure out how to share this filenow. If I zip the file, won't I be in danger of losing the data Iworked hard to preserve by usingmigratePersistentStore:toURL:options:withType:error in the firstplace? I believe so, but I do want to enable my users to share/save theirbackups vie email, etc, and I don't know how to best approach this now.
  2. I am having a hard time understanding howmigratePersistentStore:toURL:options:withType:error can be used tobackup the file to iCloud. Much like the sharing example above, Isee that I can useaddPersistentStoreWithType:configuration:URL:options:error, andmigratePersistentStore:toURL:options:withType:error to get thedesired .sqlite copy locally, but if I then try to upload that localfile to iCloud, I fear I will lose the data I worked to preserve byusing migratePersistentStore:toURL:options:withType:error in thefirst place. I have been trying to see if there is a way that Icould/should usemigratePersistentStore:toURL:options:withType:error to move thenewly created persistentStore directly to iCloud, but I haven't beenable to find any documentation on how to do that or if it should bedone at all. Is there a specific url I would need to use to indicate that the destination for the persistentStore is iCloud?

非常感谢您能就这些问题的答案分享任何见识.

I would greatly appreciate any insights that can be shared regarding the answers to these questions.

推荐答案

  1. ZIP应该是IMO的安全方式,因为它具有用于数据完整性验证的CRC数据.

尽管关于共享CoreData存储版本,您必须要小心.假设两个用户运行不同版本的应用程序,并且彼此之间共享CoreData存储. CoreData不支持开箱即用的渐进式迁移. https://www.objc.io/issues/4 -core-data/core-data-migration/

You have to be careful though regarding shared CoreData store version. Suppose two users run different versions of the app and share the CoreData store between each other. CoreData doesn't support progressive migrations out of box. https://www.objc.io/issues/4-core-data/core-data-migration/

也许共享JSON中的部分数据,并从中重新创建CoreData实体,这比共享整个图更安全,更轻松地实现了共享数据的策略.

Maybe sharing a portion of data in JSON and re-creating CoreData entities from it would be safer and easier strategy for sharing data as opposed to sharing entire graph.

  1. 您只能将文件复制到iCloud容器中并在设备之间共享,但是您不能真正直接从容器中使用它,也不能通过iCloud来进行增量更新. NSFileManager具有setUbiquitous,允许将文件移动到iCloud容器中.
  1. You can only copy file into iCloud container and share it across devices but you can't really use it directly from container or have incremental updates coming via iCloud. NSFileManager has setUbiquitous that allows to move file into iCloud container.

这篇关于如何使用当前的NSPersistentStore方法在本地和iCloud中创建可共享的Core Data .sqlite备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:19