使用NSPersistentDocument创建“文档" [英] Using NSPersistentDocument to create 'Documents'

查看:136
本文介绍了使用NSPersistentDocument创建“文档"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个使用

  • 迅速
  • CoreData
  • 以标准macOS方式工作的文档" [自定义扩展名,一个包含与该文档有关的所有数据的单个文件"/文件包装器"

这似乎是不可能的.该文档非常清楚地表明

This does not appear possible. The documentation states very clearly that

NSPersistentDocument不支持某些文档行为: 文件包装器. [..]

NSPersistentDocument does not support some document behaviors: File wrappers. [..]

这让我认为在CoreData中处理图像的常用方法-将具有允许的外部存储空间"的二进制数据保存到其他位置,然后将URL存储在数据库中-不能与NSPersistentDocument一起使用.我希望我的用户能够对我的文件"执行常规的Finder操作(复制,移至外部存储,从外部备份还原),并且需要将我的所有数据打包在一个软件包中.

which makes me think that the usual ways of dealing with images in CoreData - binary data with 'allow external storage' and save them to a different location, store the URL in the database - cannot be used with NSPersistentDocument. I want my users to be able to do the usual Finder operations on my 'file' (duplicate, move to external storage, restore from external backup) and need all my data to be in one single package.

文件存储的SQL版本在保存时会产生通常的三重堆栈-.sqlite,.sqlite-shm,.sqlite-wal-不能用作文档".

The SQL version of the file store results in the usual three-fold stack when saving - .sqlite, .sqlite-shm, .sqlite-wal - which is useless as a 'document'.

有没有我忽视的解决方案? (示例非常稀疏; Big Nerd Ranch示例无法解决; Marcus Zarra或Objc.io均未提及NSPersistentDocument).

Is there a solution I have overlooked? (examples are very sparse; the Big Nerd Ranch sample does not solve this, either; neither Marcus Zarra nor Objc.io touch on NSPersistentDocument).

推荐答案

NSPersistentDocument一起使用的唯一选项是将图像直接存储在数据库中.您的实体上需要一个Binary Data属性,但不能打开Allows External Storage选项.

The only option that will work with NSPersistentDocument the way you want it is to store the images directly in the database. You need a Binary Data attribute on your entity, but you cannot turn on the Allows External Storage option.

如果启用此选项,Core Data将根据大小决定将图像直接存储在数据库中还是直接存储在文档所在文件夹的隐藏文件夹中:

If you turn on this option, Core Data will decide - depending on the size - whether to store the image directly in the database or in a hidden folder inside the folder where your document is located:

(我在Finder中输入cmd-shift-.使该文件夹可见).该示例文档名为Test 1.doof,其中包含三个图像:

(I made the folder visible entering cmd-shift-. in the Finder). The sample document is named Test 1.doof and it contains three images:

您可以看到隐藏的文件夹.Test 1_SUPPORT/EXTERNAL DATA包含两个文件,分别是两个较大的图像(1.3 MB和494 KB).仅50 KB的第三个存储在Test 1.doof中.如果将Test 1.doof移到另一个文件夹,则隐藏的文件夹将被留下.在其他文件夹中打开文件会导致丢失两个图像.

You can see that the hidden folder .Test 1_SUPPORT/EXTERNAL DATA contains two files, which are the two bigger images (1.3 MB and 494 KB). The third one with only 50 KB is stored inside Test 1.doof. If you move Test 1.doof into another folder, the hidden folder is left behind. Opening the file in that other folder leads to two missing images.

如果将二进制数据放入与其余数据具有一一对应关系的单独实体中,那么将图像存储在数据库中并不是什么坏事,就像这样:

Storing the images inside the database is not that bad if you put the binary data into a separate entity with a one-to-one relation to the rest of the data, like so:

这样,图像不会干扰任何搜索或排序操作. NSPersistentDocument免费为您提供了许多很酷的功能,因此您应该尽可能使用它.

That way the image does not interfere with any search or sort operation. NSPersistentDocument gives you a lot of cool functionality for free, so you should use it anyway if possible.

另外两句话:

  • 如果为属性打开Allows External Storage,则不必关心URL或图像的存储位置,Core Data会为您做到这一点(但对于基于文档的应用程序不是有用的方式)./li>
  • 这些shmwal文件是临时显示的文件,有时对于没有外部存储的数据库也显示为有时".如果它们粘住了,您可以在应用关闭时安全地将其删除.
  • If you turn on Allows External Storage for an attribute, you do not have to care about URLs or where to store the images, Core Data does that for you (but not in a useful way for document-based apps).
  • These shm or wal files are temporary files that appear "sometimes", for databases without external storage as well. If they stick, you can safely remove them when you app is closed.

这篇关于使用NSPersistentDocument创建“文档"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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