简体   繁体   English

解析-Obj-C到云代码

[英]Parse - Obj-C to Cloud Code

I'm trying to grasp Parse.com's cloud code but it is very difficult. 我正在尝试掌握Parse.com的云代码,但这非常困难。 My issue is that I want to rewrite a query which manipulates an array in cloud code (javascript) 我的问题是我想重写一个查询,该查询可以在云代码(javascript)中操作数组

Here is some of the code 这是一些代码

PFQuery * quer = [PFQuery queryWithClassName:@"Spel"];
NSString * playerID = [[PFUser currentUser] objectForKey:@"fbid"];
[quer whereKey:@"lobby" containsAllObjectsInArray:@[playerID]];
[quer findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
    for (PFObject * obj in objects) {
        if (!error) {


        NSMutableArray * ready = [[obj objectForKey:@"ready"] mutableCopy];
        if (![ready containsObject:[[PFUser currentUser] objectForKey:@"fbid"]]) {
                [ready addObject:[[PFUser currentUser] objectForKey:@"fbid"]];
        }

        [obj setObject:ready forKey:@"ready"];
        [obj saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
            if (succeeded) {
            //success}

I would like to have this same code run in cloud code, since it's a multiplayer game and people click the button at the same time, there is an issue where the array is manipulated faulty. 我想在云代码中运行相同的代码,因为这是一个多人游戏,并且人们同时单击按钮,所以存在阵列操作错误的问题。

Some kind soul out there who would know how to this? 那里有些善良的灵魂会知道如何做? Since it feels a little more complex than just saving a normal object with cloud code 因为感觉比用云代码保存普通对象要复杂得多

Kind Regards, Martin 亲切的问候,马丁

It looks like the code finds Spel objects where the current user's "fbid" is in the Spel's "lobby" array. 看起来该代码在当前Spel的“大厅”数组中找到当前用户的“ fbid”所在的Spel对象。 For each one found, add the user's "fbid" to the Spel's "ready" property. 对于找到的每一个,将用户的“ fbid”添加到Spel的“ ready”属性中。

You'd say that in JS as follows: 您可以在JS中这样说:

var _ = require('underscore');

Parse.Cloud.define("myCloudFunction", function(request, response) {
    var fbid = request.user.get("fbid");
    var query = new Parse.Query("Spel");
    query.equalTo("lobby", fbid);
    query.find().then(function(results) {
        _.each(results, function(spel) {
            spel.addUnique("ready", fbid);
        });
        return Parse.Object.saveAll(results);
    }).then(function(result) {
        response.success(result);
    }, function(error) {
        response.error(error);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM