Parse iOS SDK: Calling Cloud Functions From Xcode

回眸只為那壹抹淺笑 提交于 2019-12-10 09:53:41

问题


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:


  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.



来源:https://stackoverflow.com/questions/24873261/parse-ios-sdk-calling-cloud-functions-from-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!