CoreData - multiple persistent stores [closed]
Asked Answered
P

1

6

I need some help understanding CoreData.

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.

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

Pantheon answered 2/11, 2015 at 17:6 Comment(2)
To not rubbish the memory, it's obvious that I should use in-memory storage - what does this mean? You should have the minimum amount of info in memory that you possibly can...Talavera
@Talavera what I meant was not to rubbish the device's ROM (or is it flash memory?), so I need to use RAM instead, which in-memory persistent store uses.Pantheon
B
20

In general,

If you use different entities in different persistent stores,

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.

If you use the same entity in multiple persistent stores,

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.

When moving instances between stores,

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.

I have no idea how MagicalRecord or RestKit support this, or if they even do.

Broth answered 2/11, 2015 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.