I'm working with an intermediate join table in Core Data similar to the friends example from Apple's Core Data Programming Guide. Sorry, can't post pictures yet due to no reputation points. Its a friends entity with relationships to FriendInfo intermediate join, with relationships of friends and friend info. The following link describes it.
link text
How would I get a list of mutual friends, ie persons both Person A and Person B share as friends?开发者_Go百科 I'm looking to put this into an iPhone application table view.
The answer is almost there in the documentation url you have given. Let's say there are two people personA
and personB
. You can get their respective friends by
NSSet *friendsOfPersonA = [personA valueForKeyPath:@"friends.friend"];
NSSet *friendsOfPersonB = [personB valueForKeyPath:@"friends.friend"];
Do a set intersection to find common friends
NSSet *commonFriends = [[NSMutableSet alloc] initWithSet:friendsOfPersonA];
[commonFriends intersectSet:friendsOfPersonB];
The set commonFriends
should be what you want.
Note: The example Apple gives includes an additional property like befriendedBy
. Usually that's not the case in online social networks such as Facebook where friendship is mutual. But if you were to go down that road and want to count a friendship only when two people are friends of each other, then it's one extra step to get there. To find the friends of a person in that case, you'd do a set intersection for a person's friends and those who have befriended him.
精彩评论