是否可以将多个(两个)持久存储与一个对象模型一起使用,同时保持一个与另一个之间的关系? [英] Can multiple (two) persistent stores be used with one object model, while maintaining relations from one to the other?

查看:9
本文介绍了是否可以将多个(两个)持久存储与一个对象模型一起使用,同时保持一个与另一个之间的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介

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

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

NSPersistentStoreCoordinator 的多个持久化存储

这就是我想到使用两个持久存储的原因.第一个是捆绑包中的大实体,第二个可能是文档文件夹中的小实体,用于存储与大商店有关系的特殊收藏夹"实体.

我知道在这方面有一些可能,但我找不到具体细节.如果您还有多个对象模型,是否应该只使用多个商店?一个对象模型可以分布"在两个持久存储上吗?在浏览):

[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType配置:@"ModifyInBackground"网址:storeURL1选项:选项错误:&错误][persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType配置:@"ModifyInMain"网址:storeURL2选项:选项错误:&错误]

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

注意: 获取的属性总是返回一个 NSArray,因为您为建立链接而编写的谓词可能有多个结果.如果您只想访问一个实体,您可以在 NSManagedObject 子类的包装器方法中放置类似的内容:

Wallpaper *recordedWallpaper = [record.wallpaper lastObject];

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.

解决方案

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:

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:

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];

这篇关于是否可以将多个(两个)持久存储与一个对象模型一起使用,同时保持一个与另一个之间的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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