Parse Swift: User relations with a “friends reques

2019-04-12 14:31发布

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

2条回答
Ridiculous、
2楼-- · 2019-04-12 14:59

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
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-12 15:00

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.

查看更多
登录 后发表回答