Parse iOS SDK: Calling Cloud Functions From Xcode

2019-05-17 00:17发布

Scenario
I have these two Cloud Functions that I want to use in my application. They check for the online status of the user and I want to set a boolean key "isOnline" for each user to YES if the user is online and to NO if they are not.

var moment = require("moment");

Parse.Cloud.define("registerActivity", function(request, response) {
    var user = request.user;
    user.set("lastActive", new Date());
    user.save().then(function (user) {
        response.success();
    }, function (error) {
        console.log(error);
        response.error(error);
    });
});

Parse.Cloud.define("getOnlineUsers", function(request, response) {
    var userQuery = new Parse.Query(Parse.User);
    var activeSince = moment().subtract("minutes", 2).toDate();
    userQuery.greaterThan("lastActive", activeSince);
    userQuery.find().then(function (users) {
        response.success(users);
    }, function (error) {
        response.error(error);
    });
});

Problem
I am not the best with Javascript, and because of that I need some help getting my head around what is happening/what I'm supposed to do.

Questions
1) When do I call "registerActivity" and "getOnlineUsers" inside my Xcode project?

2) Is "response.success(users)" just an array of PFUser Objects?

3) If "2)" is true, then how do I set the bool key "isOnline" for all of the users in the "response.success(users)" to YES if they are in the array?

1条回答
smile是对你的礼貌
2楼-- · 2019-05-17 00:49
  1. You would call these functions when you want to get the online users. The code for calling these would be:

    [PFCloud callFunctionInBackground:@"registerActivity" withParameters:@{@"user": Put objectId for user here}                             
    block:^(NSString *response, NSError *error) {
                                        if (!error) {
    
                                        }
                                    }];
    
    [PFCloud callFunctionInBackground:@"getOnlineUsers" withParameters:@{} 
    block:^(NSArray *users, NSError *error) { 
    if (!error) 
    {
    
    } }];
    
  2. Yes, I believe it would just be an array of PFUser objects, I would run this just to make sure, though.

  3. Once you get the response from "getOnlineUsers" you should probably send it back up to another cloud code function that uses the master key (Parse.Cloud.useMasterKey();) to access/change user objects, and change the "isOnline" field to YES.

查看更多
登录 后发表回答