解析iOS SDK:了解Cloud Code

Tom*_*ool 9 javascript objective-c ios parse-platform parse-framework

场景 =我慢慢地,但肯定地围绕着Parse的云代码功能.我只是需要那些想要回答一些关于一些示例云代码函数中发生的事情的简短而相对简单的问题的人的帮助.

我将在此示例中使用的代码如下

1)云代码

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

});
Run Code Online (Sandbox Code Playgroud)

2)从iOS调用

[PFCloud callFunction:@"editUser" withParameters:@{

    @"userId": @"someuseridhere",
    @"newColText": @"new text!"

}];
Run Code Online (Sandbox Code Playgroud)

这段代码来自这里

问题1 =

(request, response) 
Run Code Online (Sandbox Code Playgroud)

我很困惑这是什么.这就像我在iOS中的类型转换(在iOS调用中)我想将NSString传递给这个函数("userId"),在云代码函数中我会称之为"请求"吗?那是怎么回事?

问题2 =

Parse.Object.extend('_User') 
Run Code Online (Sandbox Code Playgroud)

这是从Parse数据库抓取"User"类,以便各种"PFObject"可以通过在它下面的行中创建一个新的"用户"来更新它吗?

这是......

PFObject *userObject = [PFObject objectWithClassName:@"User"]?
Run Code Online (Sandbox Code Playgroud)

问题3 =

user.set('new_col', newColText)
Run Code Online (Sandbox Code Playgroud)

这显然"设置"要保存到PFUser的值(〜我认为).我知道"newColText"变量是要设置的文本 - 但是'new_col'是什么?我唯一能想到的是,这会在数据库中设置一个新列的名称,通过"请求"传递什么类型?

这是......

[[PFUser currentUser] setObject: forKey:]
Run Code Online (Sandbox Code Playgroud)

问题4 =

Parse.Cloud.useMasterKey() 
Run Code Online (Sandbox Code Playgroud)

没有太技术化,在我可以从另一个用户编辑"用户"对象之前,这基本上是我必须输入的吗?

问题5 =

user.save().then(function(user) {
        response.success(user);
    }
Run Code Online (Sandbox Code Playgroud)

这是......

[user saveInBackgroundWithBlock:]? 
Run Code Online (Sandbox Code Playgroud)

如果是的话,是

function(error) {
        response.error(error)
Run Code Online (Sandbox Code Playgroud)

只是设置如果saveInBackgroundWithBlock中有错误会发生什么?

请记住,我知道iOS - 而不是 JavaScript.因此,尽量让对理解Apple领域的人有描述性.

Tim*_*ers 11

这是我对你的问题的看法:

  1. request参数用于访问作为请求/调用云功能的所有内容,它包括传递的参数(request.params),在客户端上验证的用户(request.user)以及您可以在文档中了解的其他一些内容.该response是你将信息发送回调用代码,您通常调用response.success()response.error()与被包含在响应中,又一个可选的字符串/对象的/ etc 文档在这里.
  2. 这是一种创建a的实例的方法User,因为它是一个特殊的内部类,_User而不是_Role_Installation.它正在创建一个具有ID的用户实例,而不是创建一个新实例(在保存之前不会有ID).以这种方式创建对象时,只需更改要更新的属性即可"修补"它.
  3. 再看一下文档示例,第一个参数是列名(如果它不存在则会创建),第二个值是您希望该列设置的值.
  4. Parse.Cloud.useMasterKey()当您需要执行登录客户端的用户无权执行的操作时,您必须执行此操作.它意味着"忽略所有安全,我知道我在做什么".
  5. 您正在看一个承诺链,链中的每一步都允许您传入"成功"处理程序和可选的"错误"处理程序.有一些很棒的文档.当你想按顺序做一些事情时,这是非常方便的,例如

示例代码:

var post = new Parse.Object('Post');
var comment = new Parse.Object('Comment');
// assume we set a bunch of properties on the post and comment here
post.save().then(function() {
    // we know the post is saved, so now we can reference it from our comment
    comment.set('post', post);
    // return the comment save promise, so we can keep chaining
    return comment.save();
}).then(function() {
    // success!
    response.success();
}, function(error) {
    // uh oh!
    // this catches errors anywhere in the chain
    response.error(error);
});
Run Code Online (Sandbox Code Playgroud)