2

At the moment, I have two Core Data models, Model A and Model B.

Model A needs a value from Model B. (Model A -> Entity1 -> valueFromModelB)

At the moment, I'm executing a fetch request and filtering in Model A's subclass of NSObject. However, this doesn't seem very efficient because I need to update this value quite often.

What would be the best way to accomplish this? Would it be better to merge the entities from Model A and Model B and create a relationship between the two? Ideally I'd like to keep these separate, but if merging is easier and more efficient, then I may go that route.

jscs
  • 63,694
  • 13
  • 151
  • 195
Brandon Schlenker
  • 5,078
  • 1
  • 36
  • 58
  • 1
    Take a look here: [cross-model-relationships-in-nsmanagedobjectmodel-from-merged-models](http://stackoverflow.com/questions/130316/cross-model-relationships-in-nsmanagedobjectmodel-from-merged-models). Maybe, It's more simple to have those entities in the same model. Hope it helps. – Lorenzo B May 17 '12 at 14:35

2 Answers2

1

From your question, was not sure if you have already looked at the option of prefetching to save some over-head. According to Core Data Documentation, here is a code snippet for prefetching.

NSManagedObjectContext *context = /* get the context */;
NSEntityDescription *employeeEntity = [NSEntityDescription
entityForName:@"Employee" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:employeeEntity];
[request setRelationshipKeyPathsForPrefetching:
[NSArray arrayWithObject:@"department"]];

The code fetches employee and department information, but what I was not sure was if Department is in a different data model, can it be put to use using something like a NSPersistentStoreCoordinator.

One other note from Apple's Core Data preformance recommendation is

Each round trip to the persistent store (each fetch) incurs an overhead, both in accessing the store and in merging the returned objects into the persistence stack. You should avoid executing multiple requests if you can instead combine them into a single request that will return all the objects you require. You can also minimize the number of objects you have in memory.

So if ideally if you can merge two different Core Data models, that is going to save memory and round-trips to fetch data.

sathish_at_madison
  • 823
  • 11
  • 34
-1

create different entities in same dataModel & create the relationship among the entities. That would be much faster & easy . :)

JainAnk
  • 205
  • 3
  • 13