嵌套核心数据提取 [英] Nested core data fetch
问题描述
我有一个包含多个实体的核心数据数据库。有一个称为人的父实体,其中有很多朋友,朋友有很多活动,而活动有类型(类型与活动有很多关系)。我想要实现的是按类型过滤所有人实体。用户将点击类型,然后刷新表格并过滤类型所显示的人实体
I have a core data DB with multiple entities. There is a parent entity called "Person" which has many "Friends", "Friends" have many "Activities", and "Activities" have "Types" ("Types has a to-many relationship to "Activities"). What I'm trying to achieve is filtering all the "Person" entities by "Types". A user would be tapping on a "Type" and then I would refresh my table and filter the "Person" entities that are displayed by the "Types" that are associated with them.
目前,我在想我必须使用复合谓词,但我完全确定该怎么做。完成后,通过遍历fetchedObjects来打印出我想要的值:
Currently I'm thinking I have to use a compound predicate but I'm completely sure how to go about it. So far all I've done is printed out the values I've wanted by looping through my fetchedObjects like so:
NSArray *persons = self.fetchedResultsController.fetchedObjects;
for (JIPerson *person in persons) {
JIFriend *friend = person.friends.anyObject;
JIActivity *activity = friend.activities.anyObject;
JIType *type = activity.type;
NSLog(@"%@", type.name);
}
这会正确打印出值,但我需要过滤我的表使用这些值。我该如何实现?
This prints out the values correctly, but I need to filter my table using these values. How can I achieve that?
推荐答案
好像我明白了。使用NSPredicate,您可以使用点表示法遍历像这样的深层关系。我的实现如下:
Seems like I got it. Using NSPredicate you can traverse a deep relationship like this using dot notation. My implementation went as follows:
- (void)filterPersonByType:(NSString *)typeName {
NSPredicate *typePredicate = [NSPredicate predicateWithFormat:@"ANY friends.activities.type.name CONTAINS[cd]%@", typeName];
}
这篇关于嵌套核心数据提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!