A "friends list" is a great example of a many-to-many relationship. Seems to me that a tutorial of how to add a "friends list" would solve both these questions, so that's what I'm doing.
Requirements
First lets think of what a "friends list" should entail:- Find a person
- Add them to my friends list
Revised Requirements
- Find a person
- Highlight if I already have them in my friends list, or have asked
- List requests from others waiting for my approve/reject action
- List my friends
- List my requests that are pending
- List my rejections
- List requests I have rejected
- Request to add someone as a friend
- Approve a request
- Reject a request
- Un-friend
- Things we could add later:
- Un-reject (change to approved, in case someone makes a mistake)
- Re-request (possibly with a time-window to prevent abuse)
Status (string: requested/approved/rejected/etc)
So, lets see how we can handle the above lists and actions:
Find a person (highlight existing status)
Once your existing "find a person" query is run, you would need an array of User objects. Using this list we could ask for all FriendRequest objects where RequestFrom is in the list or RequestTo is in the list.List requests from others waiting for my approver/reject action
List my friends
List my requests that are pending
List my rejections
List requests I have rejected
Request to add someone as a friend
First make sure there's no existing record with the current User and the target User as RequestFrom/RequestTo or the other way around.If that requirement is met then you just need to create a new FriendRequest object with the following properties:
RequestFrom: current User RequestTo: target User Status: requested
Approve a request
Change the Status from "requested" to "approved".Reject a request
Change the Status from "requested" to "rejected".Un-friend
Summary
In theory it looks like everything is covered, though I welcome comments and feedback if you think I'm missed anything or made a mistake.I'll post some sample code (JavaScript and maybe some Objective-C too) once I get my code formatting working again.