save current user to relation in parse with swift

吃可爱长大的小学妹 提交于 2019-12-11 11:01:49

问题


I have a system where when the current user accepts a request my app adds a relation between the user that sent the request to the current user.

However I want to also have the current user added to the other user's relation so that it is not just the current user that has a relation.

My code for the current user works, but not for the other user.

    //i use this pattern to add the requester to a friends relation to the current user 
    var relation = PFUser.currentUser()!.relationForKey("Friends")
    relation.addObject(passenger as PFObject)
    PFUser.currentUser()!.saveInBackground()

When I try the same pattern for the passenger it does not add the relation

      var relation = passenger.relationForKey("Friends")
      relation.addObject(PFUser.currentUser()!.self as PFObject)
      passenger.saveInBackground()

Edit 1:

After a lot of research I found this answer that says I need cloud code. https://stackoverflow.com/a/23305056/3822504 Is this the only way or is there an alternative? Could someone provide a cloud code solution?

Edit 2:

I answered my own question. You will need to edit the cloud code based on your needs. The biggest thing that helped me was knowing how to query user class in cloud code and getting the current user in cloud code.


回答1:


This is the only way, afaik. Follow this guide to set up your cloud code environment, if you haven't already: https://parse.com/apps/quickstart#cloud_code/unix

Then you should define a function (the code after set-up will contain a sample "hello world" function, so you can copy the definition from it). For relation modification you should refer to their JS api docs - https://parse.com/docs/js/api/symbols/Parse.Relation.html

(Note: In order for it to work, it should containt Parse.Cloud.useMasterKey() line before you try to modify users)

After your function is implemented and cloud code is deployed (parse deploy), use PFCloud.callFunctionInBackground in your mobile app.




回答2:


Okay so after a while I was able to get a solution for my scenario. This should work for simple relations when a user accepts another user.

First my swift code. It is important to note that I created a function that takes in a PFUser as a parameter. I did this to refactor a larger function. This user is the user we want to save the current user to in the cloud code.

func createRelation(otherUser: PFUser)
{

    let fromUser: PFUser = otherUser
    let params = NSMutableDictionary()
    params.setObject(fromUser.objectId!, forKey: "Friends")


    PFCloud.callFunctionInBackground("addFriendToFriendsRelation", withParameters: params as [NSObject : AnyObject] ) {
        (object:AnyObject?, error: NSError?) -> Void in

        //add the person who sent the request as a friend of the current user
        let friendsRelation: PFRelation = self.currentUser!.relationForKey("Friends")
        friendsRelation.addObject(fromUser)
        self.currentUser!.saveInBackgroundWithBlock({
            (succeeded: Bool, error: NSError?) -> Void in

            if succeeded {
                println("Relation added to the current user")


            } else {

                println("Handle error in adding relation to current user ")
            }
        })
    }

}

Now for the parse cloud code

    Parse.Cloud.define("addFriendToFriendsRelation", function(request, response) {

    Parse.Cloud.useMasterKey();
    //get the friend requestId from params
    var friendRequestId = request.params.Friends;
    var query = new Parse.Query(Parse.User);

    //get the friend request object
    query.get(friendRequestId, {
        success: function(friendRequest) {

            //get the user the request was from
            var fromUser = friendRequest;
            //get the user the request is to
            var toUser = Parse.User.current() ;

            var relation = fromUser.relation("Friends");
            //add the user the request was to (the accepting user) to the fromUsers friends
            relation.add(toUser);

            //save the fromUser
            fromUser.save(null, {

                success: function() {

                     response.success("saved relation and updated friendRequest");

                },

                error: function(error) {

                response.error(error);

                }

            });

        },

        error: function(error) {

            response.error(error);
        }

    });

});

Furthermore make sure you use the command parse deploy in the parse directory to update the main.js



来源:https://stackoverflow.com/questions/32419855/save-current-user-to-relation-in-parse-with-swift

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