在核心数据文档中存储文档特定设置的最佳方法是什么?

发布于 2024-07-29 05:49:09 字数 310 浏览 5 评论 0原文

我在应用程序中使用由 Core Data 支持的 NSPersistantDocument

每个文档都有许多仅与该文档相关的设置。 例如,文档需要保存地址簿组的唯一 ID,以便稍后更新。

我考虑解决这个问题的方法是使用另一个模型 DocumentSetting,它有两个属性: 一个键和一个值,其中包含 NSString 。

理想情况下,我想要类似于 NSUserDefaults 但存储在文档中的内容。

以前有人这样做过吗? 有没有更好的方法来解决这个问题?

I'm using a NSPersistantDocument backed by Core Data in my application.

Each document has a number of settings that are only relevant to that document. For example the document needs to hold the unique ID of an Address Book Group, to update it at a later date.

The way I'm thinking of approaching this is to have another model, DocumentSetting, which has two attributes; a key and a value, which hold NSString's.

Ideally I would like something similar to NSUserDefaults but stored within the document.

Has anyone done this before? Are there any better way to approach this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一桥轻雨一伞开 2024-08-05 05:49:09

NSPersistentStore 上的 setMetadata: 方法可用于存储每个文档的设置。 有关如何使用商店元数据的文档可以找到 此处。 不过,正如文档中所建议的,可以放入其中的内容有一些大小限制。 如果您需要存储比几个键/值对更重要的内容,我建议您在数据模型中创建一个新实体来存储此类数据。

The setMetadata: method on NSPersistentStore can be used to store per-document settings. The docs for how to go about using the store's metadata can be found here. As advised in the docs though, there are some size limitations on what can be put in there. If you need to store something more substantial than a few key/value pairs, I would recommend making a new entity in your data model to store such data.

最佳男配角 2024-08-05 05:49:09

正如 Brian 所说,持久存储的元数据是进行此类操作的好地方。 或者,您添加单独实体的想法也可以工作(我过去使用过这两种方法)。 请记住,如果此 DocumentSetting 实体与文档模型中的其他实体没有关系,您可以将其放入单独的 xcdatamodel 中,并使用 -[NSManagedObjectModel mergedModelFromBundles 在运行时合并模型:]。 至少这样你可以让你的“源”模型文件保持一定的隔离。

在策略方面,除非您将拥有大量此类文档设置或想要单独查询它们,否则您可以使用可转换属性并在您的 中存储(已存档的)NSDictionary >DocumentSettings 实体。 这会让感觉更像 NSUserDefaults API,它基本上就像一本字典。

As Brian says, the persistent store's metadata is a good place for this kind of thing. Alternatively, your idea to add a separate entity work as well (I've used both methods in the past). Keep in mind that if this DocumentSetting entity doesn't have relationships to other entities in your document model, you can put it into a separate xcdatamodel and merge the models at runtime using -[NSManagedObjectModel mergedModelFromBundles:]. At least this way you can keep your "source" model files isolated a bit.

In terms of strategy, unless you are going to have a very large number of these document settings or want to query them individually, you could use a transformable property and store an (archived) NSDictionary in your DocumentSettings entity. This would make the feel much more like the NSUserDefaults API which feels basically like a dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文