简体   繁体   English

问:Parse.com查询计数稳定性

[英]Q: Parse.com query count stability

For testing purpose I put the following code in the onCreate() of an Activity: 出于测试目的,我将以下代码放在Activity的onCreate()中:

    // Create 50 objects
    for (int i = 0; i < 50; i++) {
        ParseObject obj = new ParseObject("test_obj");
        obj.put("foo", "bar");
        try {
            obj.save();
        } catch (ParseException pe) {
            Log.d("Parsetest", "Failed to save " + pe.toString());
        }
    }

    // Count them
    for (int i = 0; i < 10; i ++) {
        ParseQuery<ParseObject> query = ParseQuery.getQuery("test_obj");
        query.countInBackground(new CountCallback() {
            @Override
            public void done(int count, ParseException e) {
                if (e == null) {
                    Log.d("Parsetest", "Background found " + count + " objects");
                } else {
                    Log.d("Parsetest", "Query issue" + e.toString());
                }
            }
        });
    }

I would expect the count to be always fifty, however running this code yields something like: 我希望计数总是五十,但运行此代码产生类似于:

D/Parsetest(17761): Background found 0 objects
D/Parsetest(17761): Background found 0 objects
D/Parsetest(17761): Background found 0 objects
D/Parsetest(17761): Background found 0 objects
D/Parsetest(17761): Background found 0 objects
D/Parsetest(17761): Background found 0 objects
D/Parsetest(17761): Background found 50 objects
D/Parsetest(17761): Background found 0 objects
D/Parsetest(17761): Background found 0 objects
D/Parsetest(17761): Background found 0 objects

Can somebody explain this behavior and how to correct this ? 有人可以解释这种行为以及如何纠正这个问题?

Without knowing further details, I'm inclined to believe the inconsistency is due to threading and the mixing of synchronous/asynchronous calls. 在不知道更多细节的情况下,我倾向于认为不一致是由于线程和同步/异步调用的混合。

For example, calling obj.save(); 例如,调用obj.save(); is synchronous ( reference ), however, without seeing the rest of your code, it's possible that the synchronous save is being executed on a background thread. 同步引用 ),但是,在没有看到其余代码的情况下,可能在后台线程上执行同步保存。

Additionally, query.countInBackground is asynchronous and is being called multiple times with a for loop. 另外, query.countInBackground异步的,并且使用for循环多次调用。 This is going to simultaneously create 10 separate background processes to query Parse for the count of objects and depending on how the save is handled there could be race conditions. 这将同时创建10个单独的后台进程来查询Parse以查找对象的数量,并且根据如何处理保存,可能存在竞争条件。

Lastly, there are documented limitations on count operations with Parse. 最后,对Parse的计数操作存在记录限制

Count queries are rate limited to a maximum of 160 requests per minute. 计数查询的速率限制为每分钟最多160个请求。 They can also return inaccurate results for classes with more than 1,000 objects. 它们还可以为具有1,000个以上对象的类返回不准确的结果。 Thus, it is preferable to architect your application to avoid this sort of count operation (by using counters, for example.) 因此,最好构建应用程序以避免这种计数操作(例如,通过使用计数器)。

From Héctor Ramos on the Parse Developers Google group , 来自Parse Developers Google小组的HéctorRamos

Count queries have always been expensive once you throw some constraints in. If you only care about the total size of the collection, you can run a count query without any constraints and that one should be pretty fast, as getting the total number of records is a different problem than counting how many of these match an arbitrary list of constraints. 一旦你抛出一些约束,计数查询总是很昂贵。如果你只关心集合的总大小,你可以运行一个没有任何约束的计数查询,并且一个应该非常快,因为得到记录的总数是一个不同的问题,而不是计算其中有多少匹配任意约束列表。 This is just the reality of working with database systems. 这只是使用数据库系统的现实。

Given the cost of count operations, it is possible that Parse has mechanisms in place to prevent rapid bursts of count operations from a given client. 考虑到计数操作的成本,Parse可能具有防止来自给定客户端的计数操作的快速突发的机制。

If you are needing to perform count operations often, the recommended approach is to use cloud code afterSave hooks to increment/decrement a counter as needed. 如果您需要经常执行计数操作,建议的方法是使用云代码afterSave挂钩根据需要递增/递减计数器。

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

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