使用Parse Cloud Code将用户添加到PFRelation [英] Adding a user to PFRelation using Parse Cloud Code

查看:145
本文介绍了使用Parse Cloud Code将用户添加到PFRelation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Parse.com和我的iPhone应用程序。

I am using Parse.com with my iPhone app.

我之前遇到了一个问题,我试图将当前登录的用户添加到另一个用户的PFRelation键/列名为friendsRelation,它基本上是朋友列表。

I ran into a problem earlier where I was trying to add the currently logged in user to another user's PFRelation key/column called "friendsRelation" which is basically the friends list.

唯一的问题是,除了那个用户之外,不允许将更改保存到任何其他用户目前已登录。

The only problem, is that you are not allowed to save changes to any other users besides the one that is currently logged in.

然后我了解到,您可以使用解决方法,使用主密钥和解析云代码。

I then learned, that there is a workaround you can use, using the "master key" with Parse Cloud Code.

我最后将代码添加到我的Parse Cloud Code中: https://stackoverflow.com/ a / 18651564/3344977

I ended up adding the code here to my Parse Cloud Code: https://stackoverflow.com/a/18651564/3344977

这很好用,我可以成功测试它并将NSString添加到Parse数据库中的字符串列/键。

This works great and I can successfully test this and add an NSString to a string column/key in the Parse database.

但是,我不知道如何修改Parse Cloud Code以允许我将用户添加到另一个用户的PFRelation列/密钥。

However, I do not know how to modify the Parse Cloud Code to let me add a user to another user's PFRelation column/key.

我一直在尝试使用上面的Parse Cloud Code链接到上面的Parse Cloud Code并且无法正常工作,然后我意识到我的问题在于实际的云代码,而不是我在xcode中如何使用它,因为我说我可以让它成功添加一个NSString对象用于测试目的。

I have been trying everything for the past 2 hours with the above Parse Cloud Code I linked to and could not get anything to work, and then I realized that my problem is with the actual cloud code, not with how I'm trying to use it in xcode, because like I said I can get it to successfully add an NSString object for testing purposes.

我的问题是我不懂javascript而且不懂语法,所以我不知道如何更改用javascript编写的Cloud Code。

My problem is that I do not know javascript and don't understand the syntax, so I don't know how to change the Cloud Code which is written in javascript.

我需要编辑我上面链接的Parse Cloud Code,我是也会在下面的问题末尾粘贴,以便我可以将当​​前登录的PFUser对象添加到另一个用户的PFRelation键/列。

I need to edit the Parse Cloud Code that I linked to above, which I will also paste below at the end of this question, so that I can add the currently logged in PFUser object to another user's PFRelation key/column.

我想要的代码用于在objective-c中执行此操作将是:

The code that I would use to do this in objective-c would be:

[friendsRelation addObject:user];

所以我很确定它只是将一个对象添加到数组中,但就像我一样说我不知道​​如何修改Parse Cloud Code,因为它是在javascript中。

So I am pretty sure it is the same as just adding an object to an array, but like I said I don't know how to modify the Parse Cloud Code because it's in javascript.

这是解析云代码:

Parse.Cloud.define('editUser', function(request, response) {
    var userId = request.params.userId,
        newColText = request.params.newColText;

    var User = Parse.Object.extend('_User'),
        user = new User({ objectId: userId });

    user.set('new_col', newColText);

    Parse.Cloud.useMasterKey();
    user.save().then(function(user) {
        response.success(user);
    }, function(error) {
        response.error(error)
    });
});

然后我将使用objective-c在xcode中使用它:

And then here is how I would use it in xcode using objective-c:

[PFCloud callFunction:@"editUser" withParameters:@{
    @"userId": @"someuseridhere",
    @"newColText": @"new text!"
}];

现在只需将其修改为将当前PFUser添加到另一个用户的PFRelation列/键,我很确定在技术上只是将一个对象添加到数组中。

Now it just needs to be modified for adding the current PFUser to another user's PFRelation column/key, which I am pretty sure is technically just adding an object to an array.

对于熟悉javascript的人来说这应该相当简单,所以我非常感谢你的帮助。

This should be fairly simple for someone familiar with javascript, so I really appreciate the help.

谢谢。

推荐答案

我建议你重新考虑你的数据模型,并从用户表中提取以下内容。当您规划数据模型时,尤其是NoSQL数据库时,您应该首先考虑您的查询并围绕它来规划您的结构。对于移动应用程序尤其如此,因为服务器连接成本很高,并且如果您的应用程序执行大量连接,通常会引入延迟问题。

I would recommend that you rethink your data model, and extract the followings out of the user table. When you plan a data model, especially for a NoSQL database, you should think about your queries first and plan your structure around that. This is especially true for mobile applications, as server connections are costly and often introduces latency issues if your app performs lots of connections.

在用户类中存储以下内容可以轻松实现找一个人跟随谁但是,您如何解决找到所有关注您的用户的任务?如果您处于以下关系,则必须检查所有用户。这不是一个有效的查询,并且它不能很好地扩展。

Storing followings in the user class makes it easy to find who a person is following. But how would you solve the task of finding all users who follow YOU? You would have to check all users if you are in their followings relation. That would not be an efficient query, and it does not scale well.

在规划社交应用程序时,你应该为scalabilty构建。我不知道你正在构建什么样的社交应用程序,但想象一下该应用程序是否具有弹道性并成为快速增长的成功。如果你没有为可扩展性而构建,那么它很快就会崩溃,而且你有机会失去一切,因为应用程序突然变得迟钝而因此无法使用(人们几乎没有等待移动应用程序的容忍度)。

When planning a social application, you should build for scalabilty. I don't know what kind of social app you are building, but imagine if the app went ballistic and became a rapidly growing success. If you didn't build for scalability, it would quickly fall apart, and you stood the chance of losing everything because the app suddenly became sluggish and therefore unusable (people have almost zero tolerance for waiting on mobile apps).

忘记所有先前关于一致性和规范化以及设计可扩展性的优点。

对于存储以下内容和关注者,请为这两者中的每一个使用单独的表(Parse类)。对于每个用户,存储他们遵循的所有用户名(或其objectId)的数组。为追随者做同样的事情。这意味着当您选择关注某人时,需要更新两个表:您将其他用户的用户名添加到您关注的人的数组中(在以下表中),并且还添加了您的用户名到其他用户的关注者表的数组。

For storing followings and followers, use a separate "table" (Parse class) for each of those two. For each user, store an array of all usernames (or their objectId) they follow. Do the same for followers. This means that when YOU choose to follow someone, TWO tables need to be updated: you add the other user's username to the array of who you follow (in the followings table), and you also add YOUR username to the array of the other user's followers table.

使用此方法,获取关注者和关注列表的速度非常快。

Using this method, getting a list of followers and followings is extremely fast.

看看这个Cassandra NoSQL数据库的Twitter示例实现:
https://github.com/twissandra/twissandra

Have a look at this example implementation of Twitter for the Cassandra NoSQL database: https://github.com/twissandra/twissandra

这篇关于使用Parse Cloud Code将用户添加到PFRelation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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