Question

I'm currently wrapping my head around some problem with Core Data. I have one user model in its own store that I do not have any control over, as it gets shipped with a framework. A Persistent Store Coordinator, Managed Object Model and Context for this model gets created automatically and cannot be touched. In it, this model has a single user entity

On the other hand, I have a properties model with a properties entity in it that I have complete control over. In there I store properties for some user entities in the other store. Both user and property entities have an id attribute similar to a foreign key. This model has it's own Persistent Store Cordinator, Managed Object Model and Context.

What I now want is to have the associated user entity as an attribute of the properties entity so I might be able to bind to key-paths similar to myproperty.user.someValueOfTheUserEntity (I'm aware that myproperty might be an array when using fetched properties).

However, as cross-store relationships are not supported I thought of using a weak relationship via Fetched Properties. That one would just have to match the two corresponding id attributes. I have created a Fetched Property for the user in Xcode and the required accessors in my properties entity's class file (As suggested in other questions, I'm treating the values returned by the Fetched Property as an array).

However, I'm unable to set a destination entity for the Fetched Property in Xcode, as the target entity resides in a completely different store. Would I also have to define my user entity in the properties store? If so, how does Core Data know that that entity shall be fetched not from my properties store but from the users store?

Some threads mentioned using configurations for this, but I cannot find any documentation that goes further than mentioning "use configurations for this".

Can somebody enlighten me on how to set up cross-storage fetched properties? #

Was it helpful?

Solution

You can use several persistent stores that share the same data model:

  • Use single data model (xcdatamodeld) and add all your entities
  • Create configurations (Editor/Add Configuration) for each "logical set" of entities that should be stored in separate store file
  • Assign (Drag) entities to appropriate configurations
  • Add configured persistent stores to your context (see below)
  • Configure fetched properties
// 1. Add "static", read-only store
[coordinator addPersistentStoreWithType:NSSQLiteStoreType
             configuration:@"your static configuration name goes here..."
             URL:storeUrl
             options:@{
                 NSReadOnlyPersistentStoreOption: @(YES),
                 NSInferMappingModelAutomaticallyOption : @(YES)
             }
             error:&error];
// 2. Add "dynamic", writable content
[coordinator addPersistentStoreWithType:NSSQLiteStoreType
             configuration:@"your dynamic configuration name goes here..."
             URL:storeUrl
             options:@{
                NSMigratePersistentStoresAutomaticallyOption: @(YES),
                NSInferMappingModelAutomaticallyOption : @(YES)
             }
             error:&error];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top