用swiftparsing当前用户关系

我有一个系统,当当前用户接受请求我的应用程序添加发送请求的用户之间的关系当前用户。

但是我也想把当前用户添加到另一个用户的关系中,以便它不仅仅是当前用户有关系。

我的代码为当前用户工作,但不为其他用户。

//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() 

当我为乘客尝试相同的模式时,不会添加关系

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

编辑1:

经过大量的研究,我发现这个答案说我需要云代码。 https://stackoverflow.com/a/23305056/3822504这是唯一的方法,还是有一个替代? 有人可以提供云代码解决scheme吗?

编辑2:

我回答了我自己的问题。 您将需要根据您的需要编辑云代码。 帮助我的最重要的事情就是知道如何在云代码中查询用户类,并在云代码中获取当前用户。

这是唯一的方法,afaik。 按照本指南设置您的云代码环境,如果您还没有: https : //parse.com/apps/quickstart#cloud_code/unix

那么你应该定义一个函数(设置后的代码将包含一个示例“hello world”函数,所以你可以从中复制定义)。 对于关系修改,你应该参考他们的JS api文档 – https://parse.com/docs/js/api/symbols/Parse.Relation.html

(注意:为了使其工作,在尝试修改用户之前应该包含Parse.Cloud.useMasterKey()行)

在您的function实施和部署云代码( parsing 部署 )之后,在您的移动应用程序中使用PFCloud.callFunctionInBackground。

好吧,过了一段时间,我可以为我的情况得到一个解决scheme。 这应该适用于简单的关系,当用户接受另一个用户。

首先我的swift代码。 需要注意的是,我创build了一个将PFUser作为参数的函数。 我这样做是为了重构一个更大的函数。 此用户是我们要将当前用户保存到云代码中的用户。

 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 ") } }) } } 

现在parsing云代码

  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); } }); }); 

此外,确保在parsing目录中使用命令parse deploy来更新main.js