0

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? #

BinaryBucks
  • 976
  • 7
  • 17

1 Answers1

0

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];
Sergiy Salyuk
  • 325
  • 2
  • 7
  • Thanks for your response an help on this. However, now when now adding the read-only store an "The model used to open the store is incompatible with the one used to create the store" error is raised. The mom is created from the .xcdatamodel file that contains all entities in their configurations. Just adding the dynamic store does not raise the problem and also creates a .sqlite file with tables for all entities. Just adding the static store does not work. I've created a more detailed Gist (https://gist.github.com/3296730) with my code. Any ideas? – BinaryBucks Aug 08 '12 at 17:20
  • 1
    Probably that means that you have to recreate the store file (i.e. sqlite database) using the latest model. My code: `NSString *path = [[NSBundle mainBundle] pathForResource:@"MyModel" ofType:@"momd"]; NSURL *momURL = [NSURL fileURLWithPath:path]; _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];` P.S. sorry for the very long delay with reply – Sergiy Salyuk Aug 21 '12 at 10:48