This issue may have be raised here before, but I am not confident that I have understood the solution. Here is the problem, in Objective-C, Core Data, for iOS.
I am going to model a twitter-like User-Following relationship, One user can follow many, and be followed by many. The Following itself has attributes such as createTime, pending, and level – that means, I cannot merely let User has bidirectional relationships like follower and followed, like :
User <<->> User
but add one more entity, Following, with relationships like following and followed.
User <->> Following <<-> User
Here are two questions:
-
Is this the best practice?
-
If so, how to write a [aUser valueForKeyPath: …]; to fetch his/her followers or friends (being followed).
Is this the best practice?
I don’t think this problem falls into the category of “situations for which a best practice has been established”, but it seems like a reasonable solution.
If so, how to write a [aUser valueForKeyPath: …]; to fetch his/her followers or friends (being followed).
Let’s say that
Followinghas afollowerproperty for the person doing the following, and aleaderproperty for the person being followed. Also,Userhas aleadsproperty for theFollowingrelationships with people who follow a user and afollowsproperty for theFollowingrelationships with people that the user follows. Finally,Useralso has anameproperty.Given all that, to get the names of aUser’s followers, you could say:
NSSet *followers = [aUser valueForKeyPath:@”leads.follower.name”];
and to get the names of people aUser follows:
NSSet *leaders = [aUser valueForKeyPath:@”follows.leader.name”];
It seems a little odd at first that you’d get sets back from those key paths since the last two keys in each path are singular. However, since the
leadsproperty returns a set ofFollowingobjects,leads.followerreturns a set containing the users for each of those objects, andleads.follower.namereturns a set containing the names of the users of those objects.