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

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

问题描述

简介



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



如果您希望商店具有读写功能,请将其复制到应用程序的文档文件夹。我不想这样做,因为那么应用程序将是大小的两倍,而该数据库的主要部分是只读的。

NSPersistentStoreCoordinator的多个持久性商店



这就是为什么我想使用两个永久存储。第一个是文件夹中的大文件夹,第二个文件夹可能是文件夹中的一个小文件夹,存储与大商店有关系的特殊收藏实体。



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

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

[ persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
配置:@ModifyInMain
URL:storeURL2
选项:options
错误:&错误]
/ pre>

最后,当你想从存储B中的实体到存储A中的实体时,你触发获取的属性就像触发一个错误一样通过访问它。



注意:读取的属性总是返回一个NSArray,因为您写入以建立链接的谓词可能有多个结果。如果你只想得到一个实体,你可以在 NSManagedObject 子类的包装方法中放置这样的东西:

 壁纸* 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天全站免登陆