什么会导致 "Request timed out" 中的 parse.com 云代码计数?

What would cause "Request timed out" in parse.com cloud code count?

我的一个云函数偶尔会超时。虽然 class 中只有大约 700 个对象,但它似乎无法计数。如果有任何有关如何调试此问题的提示,我将不胜感激。

云功能大部分时间都能正常工作。

记录的示例错误:

E2015-02-03T02:21:41.410Z] v199: Ran cloud function GetPlayerWorldLevelRank for user xl8YjQElLO with:
  Input: {"levelID":60}
  Failed with: PlayerWorldLevelRank first count error: Request timed out

下面的代码有什么看起来很奇怪的地方吗?超时错误通常在第二个计数(query3)中抛出,尽管有时它在第一个计数(query2)中超时。

Parse.Cloud.define("GetPlayerWorldLevelRank", function(request, response) {    
    var query = new Parse.Query("LevelRecords");
    query.equalTo("owner", request.user);
    query.equalTo("levelID", request.params.levelID);
    query.first().then(function(levelRecord) {
        if (levelRecord === undefined) {
            response.success(null);
        }
        // if player has a record, work out his ranking
        else {
            var query2 = new Parse.Query("LevelRecords");
            query2.equalTo("levelID", request.params.levelID);
            query2.lessThan("timeSeconds", levelRecord.get("timeSeconds"));
            query2.count({
                success: function(countOne) {
                    var numPlayersRankedHigher = countOne;

                    var query3 = new Parse.Query("LevelRecords");
                    query3.equalTo("levelID", request.params.levelID);
                    query3.equalTo("timeSeconds", levelRecord.get("timeSeconds"));
                    query3.lessThan("bestTimeUpdatedAt", levelRecord.get("bestTimeUpdatedAt"));
                    query3.count({
                        success: function(countTwo) {
                            numPlayersRankedHigher += countTwo;
                            var playerRanking = numPlayersRankedHigher + 1;                    
                            levelRecord.set("rank", playerRanking);
                            // The SDK doesn't allow an object that has been changed to be serialized into a response.
                            // This would disable the check and allow you to return the modified object.
                            levelRecord.dirty = function() { return false; };    
                            response.success(levelRecord);    
                        },
                        error: function(error) {
                            response.error("PlayerWorldLevelRank second count error: " + error.message);
                        }                            
                    });
                },
                error: function(error) {
                    response.error("PlayerWorldLevelRank first count error: " + error.message);
                }
            });
         }
    });
});

我认为问题不在您的代码中。就像错误消息指出的那样:请求超时。即ParseAPI在超时时间内没有响应或者网络导致超时。一旦您执行 .count 一些 API 调用可能已完成,然后无法连接或超时。

显然有更多人有此问题:https://www.parse.com/questions/ios-test-connectivity-to-parse-and-timeout-question。似乎无法增加超时时间,因此 post 中的建议指出:

For that reason, I suggest setting a NSTimer prior to executing the query, and invalidating it when the query returns. If the NSTimer fires before being invalidated, ask the user if they want to keep waiting for the results to come back, or show them a message indicating that the request is taking a long time to complete. This gives the user the chance to wait more if they know their current network conditions are not ideal.

如果您要处理网络问题,尤其是在移动平台上,您需要为网络问题做好准备。所以就像 post 建议的那样:为用户提供重试的选项。