CoreData - 多个持久存储 [英] CoreData - multiple persistent stores
问题描述
我需要一些帮助理解CoreData。
I need some help understanding CoreData.
假设我安装了MagicalRecords和RestKit,我也有一个服务器,它可以返回一些帖子 。然后,我需要保存一些
帖子
到设备内存,所以用户可以稍后阅读该帖子(即使没有互联网连接)。为了不浪费内存,很明显,我应该使用内存存储,我这样做。但是为了保存帖子我definitley想要SQLite或某物,它使我真的无奈。
Let's say I have MagicalRecords and RestKit installed, and I also have a server, which can return me some posts
. Then I need to save some of those posts
onto device memory, so a user can read the post later on (even with no internet connection). To not rubbish the memory, it's obvious that I should use in-memory storage, which I do. But for saving the posts I definitley want SQLite or something, and it makes me really helpless.
问题是,苹果没有给出很多关于如何处理这种排序的信息的情境,和谷歌没有帮助到目前为止。我猜我必须做两个永久存储和两个配置:默认和保存一个。我找到了一种方法来创建两个永久性存储,但现在我只是不明白如何使用:如何创建和保存在确切存储我想要的,如何传输它们在商店之间,这将如何与RK和MR进行交互?
任何帮助将被淹没。链接,信息,代码段
The problem is, Apple do not give much information about how to handle this sort of situatuion, and the googling did not help so far. I'm guessing that I have to make two persistent stores and two configurations: the default and the "saving" ones. I have found a way to make two persistent stores, but now I just don't understand how to work with that: how to create and save enteties in exact store I want, how to transfer them between stores, how will this interact with RK and MR?
Any help will be aprichiated. Links, info, code snippets
推荐答案
一般来说,
使用不同的命名配置设置数据模型。每个配置定义包含一个或多个实体的模型的子集。您可以在模型编辑器中执行此操作。当通过调用 addPersistentStoreWithType:configuration:URL:options:error:
添加持久存储时,为第二个参数提供配置名称。
Set up your data model with different named configurations. Each configuration defines a subset of the model that contains one or more of the entities. You do this in the model editor. When you add a persistent store by calling addPersistentStoreWithType:configuration:URL:options:error:
, provide a configuration name for the second argument. Any time you insert a new instance, it will go to the correct persistent store, based on the configuration.
不要打扰配置。相反,在插入新实例后,使用 NSManagedObjectContext
的方法 assignObject:toPersistentStore:
Don't bother with configurations. Instead, after you insert a new instance, use NSManagedObjectContext
's method assignObject:toPersistentStore:
to tell it which persistent store file to use.
完全由您自己决定。没有直接的框架支持。您不能使用 assignObject:toPersistentStore:
将实例重新分配给不同的存储文件。您必须在不同的存储区中创建一个新实例,并自己复制所有属性值和关系。
It's all up to you. There is no direct framework support. You cannot use assignObject:toPersistentStore:
to reassign an instance to a different store file. You would have to create a new instance in a different store and copy over all attribute values and relationships yourself.
我不知道MagicalRecord或RestKit如何支持这个,他们甚至做。
I have no idea how MagicalRecord or RestKit support this, or if they even do.
这篇关于CoreData - 多个持久存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!