簡體   English   中英

解析-Obj-C到雲代碼

[英]Parse - Obj-C to Cloud Code

我正在嘗試掌握Parse.com的雲代碼,但這非常困難。 我的問題是我想重寫一個查詢,該查詢可以在雲代碼(javascript)中操作數組

這是一些代碼

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}

我想在雲代碼中運行相同的代碼,因為這是一個多人游戲,並且人們同時單擊按鈕,所以存在陣列操作錯誤的問題。

那里有些善良的靈魂會知道如何做? 因為感覺比用雲代碼保存普通對象要復雜得多

親切的問候,馬丁

看起來該代碼在當前Spel的“大廳”數組中找到當前用戶的“ fbid”所在的Spel對象。 對於找到的每一個,將用戶的“ fbid”添加到Spel的“ ready”屬性中。

您可以在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