Nested core data fetch

北慕城南 提交于 2019-12-12 02:45:31

问题


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.

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?


回答1:


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];
}



回答2:


Yes, you can use dot notation with NSPredicate. You might want to have the type be an actual type, though, that would be cleaner than just doing string comparisons.



来源:https://stackoverflow.com/questions/20335838/nested-core-data-fetch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!