多个(两个)持久存储可以与一个对象模型一起使用,同时保持一个对象模型与另一个对象模型之间的关系吗?

发布于 2024-10-20 23:33:13 字数 1187 浏览 3 评论 0原文

简介

我的 iOS 项目附带了一个 SQLite 格式的核心数据持久存储,大小约为 160MB。那里有大量的分组信息,用户应该能够在其中标记最喜欢的信息。为此,我需要(至少部分)数据库具有写入功能。当然,应用程序包中提供的持久存储在设计上是只读的。

如果您希望商店具有读写功能,您应该将其复制到例如应用程序的文档文件夹中。我不想这样做,因为这样应用程序的大小将是原来的两倍,而该数据库的主要部分无论如何都是只读的。那会浪费资源。

NSPersistentStoreCoordinator 的多个持久存储

这就是我考虑使用两个持久存储的原因。第一个可能是捆绑包中的大实体,第二个可能是文档文件夹中的小实体,用于存储与大商店有关系的特殊“最喜欢”实体。

我知道这方面有可能,但我找不到具体细节。如果您也有多个对象模型,是否应该只使用多个存储?一个对象模型可以“分布”在两个持久存储上吗?当浏览核心数据编程文档时,我可以没有找到任何关于如何设置它的真正参考。另外,Marcus Zarra 的书似乎没有深入探讨这个主题:

可以向 NSPersistentStoreCoordinator 添加多个 NSPersistentStore,这在处理拆分为多个文件的数据时非常有用。然而,在我们的示例中,我们只有一个文件。 (Marcus Zarra:“核心数据 - Apple 用于在 Mac OS X 上保存数据的 API”第 71 页

问题

谁可以告诉我我的想法是否可以通过核心数据和多个持久存储来实现?您能否提供有关如何实现这一目标的提示?涉及该主题的在线/离线资源也非常受欢迎。

Introduction

My iOS project ships with a Core Data persistent store weighing some 160MB in SQLite format. There is a ton of grouped information in there, in which users should be able to mark favorites. For this, I need (at least part of) the database to have write capabilities. But of course persistent stores that ship in the application bundle are by design read-only.

If you want the store to have read-write capabilities, you should copy it to, e.g. the app's documents folder. I don't want to do this, because then the app would be twice the size, while the main part of that database is read-only anyway. That would be a waste of resources.

Multiple persistent stores for NSPersistentStoreCoordinator

This is why I thought of using two persistent stores. The first would be the big one in the bundle, and the second could be a small one in the documents folder, storing special "favorite" entities with relationships to the big store.

I know something is possible in this regard, but I can't find the specifics. Should one only use multiple stores if you also have multiple object models? Can one object model be 'distributed' over two persistent stores? When browsing through the Core Data Programming docs, I can't find any real reference about how to set this up. Also Marcus Zarra's book doesn't seem to delve into this topic:

It is possible to add more than one NSPersistentStore to the NSPersistentStoreCoordinator, which can be useful when dealing with data that is split into multiple files. However, in our exam- ple, we have a single file. (Marcus Zarra: "Core Data - Apple's API for Persisting Data on Mac OS X" page 71)

The Question

Who could tell me if what I'm thinking of is possible with Core Data and multiple persistent stores? And could you maybe provide a hint about how to achieve this? Online/offline resources that deal with the topic are very much appreciated too.

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

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

发布评论

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

评论(3

ˉ厌 2024-10-27 23:33:13

答案是肯定的。 @Caleb 指出了正确的资源,但让它发挥作用仍然相当尴尬。我想我应该在这里放置一个简历:

对于两个 NSPercientStore 实例共享相同的模型,您必须向模型添加一个配置,它是实体的字符串命名子集:

模型配置

在模型中,向属于第二个商店的实体添加一个获取的属性 (NSFetchedPropertyDescription 用于谷歌搜索)。这是一个非常简单的存储过程,它可能如下所示:

NSPredicate format for the fetched property

然后,当将商店添加到持久性商店协调器中,使用 configuration 参数的字符串(更多有关此处选项的信息):

[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                          configuration:@"ModifyInBackground" 
                                                    URL:storeURL1
                                                options:options
                                                  error:&error]

[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                          configuration:@"ModifyInMain"
                                                    URL:storeURL2 
                                                options:options 
                                                  error:&error]

最后,当您想从实体中获取将 B 存储到 A 中的实体,您就可以像触发故障一样触发所获取的属性,只需访问它即可。

注意: 获取的属性始终返回 NSArray,因为您编写的用于建立链接的谓词可能有多个结果。如果您只想访问一个实体,您可以将类似的内容放入 NSManagedObject 子类的包装方法中:

Wallpaper *recordedWallpaper = [record.wallpaper lastObject];

The answer is yes. @Caleb points to the right resources, but getting it to work is still quite awkward. I thought I'd place a resumé here:

For two NSPersistentStore instances to share the same model, you have to add a configuration to your model, which is a string-named subset of the entities:

Model configurations

In the model, to an entity that belongs to the second store, you add a fetched property (NSFetchedPropertyDescription for googlability). This is somewhat of a very simple stored procedure, and it could look like this:

NSPredicate format for the fetched property

Then, when you add the stores to your persistent store coordinator, you use the strings for the configuration argument (more info about the options here):

[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                          configuration:@"ModifyInBackground" 
                                                    URL:storeURL1
                                                options:options
                                                  error:&error]

[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                          configuration:@"ModifyInMain"
                                                    URL:storeURL2 
                                                options:options 
                                                  error:&error]

Finally, when you want to get from the entity in store B to the entity in store A, you trigger the fetched property like you would trigger a fault, just by accessing it.

Note: A fetched property always returns an NSArray, because the predicate you write to establish the link might have multiple results. If you want to get to just one entity, you could place something like this in a wrapper method of your NSManagedObject subclass:

Wallpaper *recordedWallpaper = [record.wallpaper lastObject];
放低过去 2024-10-27 23:33:13

是的,您可以对单个模型使用多个存储,但无法在不同存储中的对象之间创建关系。查找 跨存储关系部分,基本上说明了这一点,并且如果您需要将一个存储中的对象与另一个存储中的对象相关联,建议使用获取的属性。

Yes, you may use multiple stores for a single model, but you can't create relationships between objects in different stores. Look for the Cross Store Relationships section in Core Data Programming guide, which says essentially that and recommends using fetched properties if you need to relate an object in one store to an object in another.

终难遇 2024-10-27 23:33:13

一种想法:您可能希望完全创建不同的存储,并为每个存储创建不同的持久存储协调器。然后为每个模型部分创建不同的托管对象上下文。假设我有一个包含 3 个实体的模型:学生、学院和课程。假设我想将 Student 和 College 实体存储在 store1 中,将 Course 存储在 Store2 中,我将有 2 组 ManagedObjectContext、持久存储和持久协调器。现在考虑到我们不能有任何跨存储关系,一个上下文中的修改不会影响另一个上下文。您不必创建不同的模型或将它们与商店相关联等。

One thought: You might want to create different stores altogether and also different persistent store coordinators for each of the store. And then create different managed object contexts for each of model part. So let us say, I have a model with 3 entities: Student, college and courses. Suppose that I want to store Student and college entities in store1, and Course in Store2, I would have 2 sets of managedObjectContext, pesistent store and persistent cordinator. Now given that we can not have any cross store relatioinships, modification in one context do not effect another context. You dont have to create different models or associate them to stores -etc.

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