迅速将当前用户保存到关系以进行解析 [英] save current user to relation in parse with swift

查看:86
本文介绍了迅速将当前用户保存到关系以进行解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

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

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.

推荐答案

好了,过了一会儿,我就能为我的场景找到解决方案.当一个用户接受另一个用户时,这应该适用于简单的关系.

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.

首先是我的快速代码.重要的是要注意,我创建了一个以PFUser作为参数的函数.我这样做是为了重构更大的功能.该用户是我们要将当前用户保存到云代码中的用户.

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

}

现在要解析云代码

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

    });

});

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

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

这篇关于迅速将当前用户保存到关系以进行解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆