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

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

问题描述

我在 iPhone 应用中使用 Parse.com.

I am using Parse.com with my iPhone app.

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

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.

然后我了解到,您可以使用一种解决方法,将主密钥"与 Parse Cloud Code 结合使用.

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

我最终将此处的代码添加到我的 Parse Cloud 代码中: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.

过去 2 小时我一直在尝试使用我链接的上述 Parse Cloud 代码进行所有操作,但无法获得任何工作,然后我意识到我的问题在于实际的云代码,而不是我的方式尝试在 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 代码,我也会在这个问题的末尾粘贴它,以便我可以将当​​前登录的 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 代码,因为它是在 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.

在规划社交应用程序时,您应该构建可扩展性.我不知道您正在构建什么样的社交应用程序,但想象一下,如果该应用程序变得异常火爆并迅速取得成功.如果您没有为可扩展性而构建,它会很快崩溃,并且您有可能失去一切,因为应用程序突然变得缓慢并因此无法使用(人们对等待移动应用程序的容忍度几乎为零).

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

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

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

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天全站免登陆