目前,我有两个核心数据模型,模型 A 和模型 B。
模型 A 需要来自模型 B 的值。(模型 A -> Entity1 -> valueFromModelB)
目前,我正在执行一个获取请求并在模型 A 的 NSObject 子类中进行过滤。但是,这似乎不是很有效,因为我需要经常更新这个值。
实现这一目标的最佳方法是什么?合并模型 A 和模型 B 中的实体并在两者之间建立关系会更好吗?理想情况下,我希望将它们分开,但如果合并更容易、更有效,那么我可能会走那条路。
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.
在同一个数据模型中创建不同的实体并创建实体之间的关系。那会更快更容易。:)