简体   繁体   English

PARSE-处理并发请求?

[英]PARSE - Handling concurrent requests?

I've created a turn based drawing game using parse where you can potentially play with anyone. 我使用解析创建了一个基于回合的绘图游戏,您可以在其中与任何人玩游戏。 Only one user at a time can play. 一次只能有一个用户可以玩。 There are three turns. 有三回合。 It's all working fine within a player's friend list -> You either start a new game, or receive a notification for you to continue one. 在玩家的朋友列表中,一切正常->您开始新游戏,或收到通知您继续游戏的信息。

I'm trying to implement a multiplayer mode -> For that I have a seperate list in a database with all available public games. 我正在尝试实现多人游戏模式->为此,我在数据库中有单独的列表,其中包含所有可用的公共游戏。 If a game is made public, I create a new object in that list with a pointer to the game. 如果将某个游戏公开,那么我会在该列表中创建一个新对象,并带有指向游戏的指针。 Every time a user searches for an available game, he queries that list, if a record is found, I delete the public game object in this particular list to make it unavailable for other users, and the game goes on.. 每次用户搜索可用的游戏时,他都会查询该列表,如果找到了记录,我会删除此特定列表中的公共游戏对象,以使其对其他用户不可用,然后游戏继续进行。

I'm wondering the following -> how does Parse handle concurrent requests? 我想知道以下->解析如何处理并发请求? Is it possible that multiple users, searching at the same time, will try to delete the same object (see code below)? 是否可能有多个用户同时搜索,试图删除同一对象(请参见下面的代码)? I'm already skipping a random index in my query to avoid such behaviour but it seems trivial.. 我已经在查询中跳过了随机索引来避免这种行为,但这似乎很琐碎。

Here is a sample of my cloud code for handling the request: 这是我的云代码示例,用于处理请求:

Parse.Cloud.define('findPublicGame', function(req, res) {
    var query = new Parse.Query("public");
    query.count({
        success: function(number) {
            if (number == 0) {
                // no public games - reject
                res.success(number);
            } else {
                // public game found - handle
                query.limit(1)
                var randomIndex = parseInt(Math.random() * number);
                query.skip(randomIndex);
                query.find({
                    success: function(object) {
                        // delete public
                        object[0].destroy({
                            success: function(_data) {
                                res.success(_data);
                            },
                            error: function(error) {
                                res.error();
                            }
                        });
                    },
                    error: function(error, object) {
                        res.error(error);
                    }
                });
            }
        },
        error: function(error) {
            res.error(error);
        }
    });
});

Is it correct to assume that trying to destroy an already-destroyed object will result in an error? 假设破坏一个已经被破坏的物体会导致错误,这是否正确?

I had an idea of adding a beforeDelete function on the public game object, to check if it's still available, but I'm trying to keep my number of requests low for now.. 我有一个想法,在公共游戏对象上添加一个beforeDelete函数,以检查它是否仍然可用,但是我现在试图将请求数量保持在较低水平。

If anyone could enlighten me, kudos! 如果有人能启发我,我就很荣幸!

Is it correct to assume that trying to destroy an already-destroyed object will result in an error? 假设破坏一个已经被破坏的物体会导致错误,这是否正确?

In my experience, no, this is not correct. 以我的经验,不,这是不正确的。 I always used the delete methods available directly in the Android and iOS APIs rather than calling to a method in the parse cloud, but for the delete methods in both Android and iOS APIs, calling delete on an already deleted object will not throw an error. 我一直使用直接在Android和iOS API中可用的delete方法,而不是在解析云中调用方法,但是对于Android和iOS API中的delete方法,在已经删除的对象上调用delete不会引发错误。 It just proceeds as if the delete were successful. 它就像删除成功一样继续进行。

I had an idea of adding a beforeDelete function on the public game object, to check if it's still available, but I'm trying to keep my number of requests low for now.. 我有一个想法,在公共游戏对象上添加一个beforeDelete函数,以检查它是否仍然可用,但是我现在试图将请求数量保持在较低水平。

This is the best option you have. 这是您最好的选择。 Typically, you're dealing with "object modification" so you would check something like a "modified at" timestamp, but since you are deleting objects, you simply need to check whether it still exists or not. 通常,您正在处理“对象修改”,因此您将检查类似“修改时”的时间戳,但是由于要删除对象,因此只需要检查对象是否仍然存在。

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

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