问题
I am kind of new to programming and parse, but I am really interested in it. I already know how to create a PFUser and how to set relations between Users, thanks to the tutorial provided on parse.com. The thing is they only have examples for one User following another User. I would prefer sending like a "Friends Request", for example like in instagram if a profile is not open to anybody. How can I code that? What do I need to think of? Example Code is very welcome :P
回答1:
I would definitely create a FriendRequest table as @MatthewLuiHK suggested. However, I don't know if this is the best way to track who is following who, this would just allow for tracking open friend requests. In my apps I use a cloud code function to set a friends
array in each user's object, as you cannot modify other users without the master key. Below is the function I use:
Parse.Cloud.define("handleFriendRequest", function(request, response) {
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.User);
//console.log(request.user);
query.get(request.params.fromUserId).then(function(fromUser) {
console.log("Hello World!");
fromUser.addUnique("friends", request.user);
return fromUser.save();
}).then(function(savedFromUser) {
response.success("Successfully made a new friend! :)");
}, function(error) {
response.error("Error occurred: " + error.message);
});
});
Before calling this function, you need to add the fromUser
pointer to the array in the current users friends
column, then send in the fromUser objectId
into this function under the "fromUserId" key in the dictionary.
回答2:
It's relatively simple.
For register or signin, call the PFUser's class method:
PFUser.signUpInBackgroundWithBlock({...})
If you want user a follow user b, you can create a follow class which would be more efficient in the future.
let follow = PFObject(className:"MYAPPFollow")
follow["fromUser"] = userA
follow["toUser"] = userB
follow.save // remember to save your newly created item
来源:https://stackoverflow.com/questions/33685487/parse-swift-user-relations-with-a-friends-request