Parse Swift: User relations with a “friends request”

坚强是说给别人听的谎言 提交于 2019-12-04 17:18:51

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.

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