iosxcodeswiftparse-platformpfrelation

save current user to relation in parse with swift


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.


Solution

  • 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