简体   繁体   中英

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..

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. 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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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