Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse iOS SDK: Understanding Cloud Code

Scenario = I am slowly but surely wrapping my head around what is going on with Parse's cloud code features. I just need some help from those who would like to answer some short, relatively simple questions about what is going on in some sample cloud code functions.

The code I will use in this example is below

1) cloud code

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

});

2) called from iOS

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

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

}];

This code was taken from here

Question 1 =

(request, response) 

I am confused by what this is. Is this like typecasting in iOS where I am saying (in the iOS call) I want to pass an NSString into this function ("userId") and inside the cloud code function I'm going to call it "request"? Is that what's going on here?

Question 2 =

Parse.Object.extend('_User') 

Is this grabbing the "User" class from the Parse database so that a "PFObject" of sorts can update it by creating a new "user" in the line below it?

Is this like a...

PFObject *userObject = [PFObject objectWithClassName:@"User"]?

Question 3 =

user.set('new_col', newColText)

This obviously 'sets' the values to be saved to the PFUser (~I think). I know that the "newColText" variable is the text that is to be set - but what is 'new_col'? Only thing I can think of is that this sets the name of a new column in the database of whatever type is being passed through the "request"?

Is this like a...

[[PFUser currentUser] setObject: forKey:]

Question 4 =

Parse.Cloud.useMasterKey() 

Without getting too technical, is this basically all I have to type before I can edit a "User" object from another User?

Question 5 =

user.save().then(function(user) {
        response.success(user);
    }

Is this like a...

[user saveInBackgroundWithBlock:]? 

and if so, is

function(error) {
        response.error(error)

just setting what happens if there is an error in the saveInBackgroundWithBlock?

Please keep in mind, I know iOS - not JavaScript. So try to be as descriptive as possible to someone who understands the Apple realm.

like image 697
Tom Testicool Avatar asked Jul 28 '14 19:07

Tom Testicool


2 Answers

Here's my take on your questions:

  1. The request parameter is for you to access everything that is part of the request/call to your cloud function, it includes the parameters passed (request.params), the User that is authenticated on the client (request.user) and some other things you can learn about in the documentation. The response is for you to send information back to the calling code, you generally call response.success() or response.error() with an optional string/object/etc that gets included in the response, again documentation here.
  2. That's a way of creating an instance of a User, which because it is a special internal class is named _User instead, same with _Role and _Installation. It is creating an instance of the user with an ID, not creating a new one (which wouldn't have an ID until saved). When you create an object this way you can "patch" it by just changing the properties you want updated.
  3. Again, look at the documentation or an example, the first parameter is the column name (it will be created if it doesn't exist), the second value is what you want that column set to.
  4. You have to do Parse.Cloud.useMasterKey() when you need to do something that the user logged into the client doesn't have permission to do. It means "ignore all security, I know what I'm doing".
  5. You're seeing a promise chain, each step in the chain allows you to pass in a "success" handler and an optional "error" handler. There is some great documentation. It is super handy when you want to do a couple of things in order, e.g.

Sample code:

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);
});
like image 162
Timothy Walters Avatar answered Sep 21 '22 21:09

Timothy Walters


I'm pretty much at the same place as you are, but here are my thoughts:

  1. No, these are the parameters received by the function. When something calls the editUser cloud function, you'll have those two objects to use: request & response. The request is basically what the iOS device sent to the server, and response is what the server will send to the iOS device.
  2. Not quite that. It's like creating a subclass of _User.
  3. Think of Parse objects types as a database table and it's instances as rows. The set will set (derp) the value of 'newColText' to the attribute/column 'new_col'.
  4. Not sure, never used that function as I don't handle User objects. But might be that.
  5. Pretty much that. But it's more sort of like (pseudo-code, mixing JS with Obj-C):

[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error){ if(error){ response.error(error); // mark the function as failed and return the error object to the iOS device } else{ response.success(user); // mark the function call as successful and return the user object to the iOS device } }];

like image 36
B.R.W. Avatar answered Sep 19 '22 21:09

B.R.W.