解析countInBackground()返回-1

时间:2015-08-04 04:26:22

标签: android parse-platform

我正在使用EditText从用户那里获取输入并将其值存储在String变量“d”中,我使用解析作为后端,现在点击按钮我正在检查表中是否存在“d”或者使用countInBackground方法,但是即使“d”存在,done()的“arg0”参数返回-1。

代码 -

final ProgressDialog pd = new ProgressDialog(Register.this);
                pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                pd.setMessage("Please wait!!");
                pd.show();
                final ParseObject po = new ParseObject("Register");
                ParseQuery<ParseObject> query = ParseQuery
                        .getQuery("Register");
                query.whereEqualTo("contact", d);
                query.countInBackground(new CountCallback() {

                    @Override
                    public void done(int arg0, ParseException arg1) {
                        pd.dismiss();
                        Toast.makeText(Register.this, d + arg0, Toast.LENGTH_LONG).show();
                        }

1 个答案:

答案 0 :(得分:0)

使用Count操作解析非常糟糕。正如他们在文档中所说:

  

计数查询的速率限制为每分钟最多160个请求。它们还可以为具有1,000个以上对象的类返回不准确的结果。因此,最好设计您的应用程序以避免这种计数操作(例如,通过使用计数器。)

我将添加结果也可以......永远不会被返回(这可能解释了你的-1)。我在一个带有3k数据表的afterSave钩子中遇到了问题,一切运行正常,直到解析停止发送响应......

最好的方法是在Register类上编写一个Cloud Code函数,它将更新一个计数器,以及一个AfterSave钩子:

首先,您可以创建一个包含2个字段的新类名Counter

+--------+-------+
|  Type  | Name  |
+--------+-------+
| String | name  |
| Number | count |
+--------+-------+

在里面添加这样的几行(将dx替换为您在应用中搜索所需的值):

+---------------------+-------+
|        Name         | Value |
+---------------------+-------+
| register_contact_d1 |     0 |
| register_contact_d2 |     0 |
| register_contact_d3 |     0 |
| ...                 |     0 |
+---------------------+-------+

然后你需要创建beforeSave Hook以在创建新对象时更新计数器:

Parse.Cloud.beforeSave('Register', function (request, response) {
  var register = request.object;

  if (register.isNew()) {
    var contact = register.get('contact');
    // we increment the counter
    updateContactCounter(contact, 1, function () {
      response.success();
    });
  } else if (register.dirty('contact') && request.master === false) {
     // We block all unwanted modification of contact as we need to be able to decrement its previous value. We check for the masterKey as we gonna use it in the Cloud Code function to update the object
     response.error();
  } else {
     response.success();
  }
});

然后我们执行Cloud Code功能

// Takes the Register id as rId, and the new contact value as newValue
Parse.Cloud.define('updateContact', function (request, response) {
    var rId = request.params.rId;
    var newValue = request.params.newValue;

    // We get the object
    var Register = Parse.Object.extend('Register');
    var query = new Parse.Query(Register);
    query.get(rId, {
      success: function(register) {
        // todo handle when oldValue is not set
        var oldValue = register.get('contact');

        // We decrement the old counter
        updateContactCounter(oldValue, -1, function () {
          // We increment the new counter
          updateContactCounter(newValue, 1 function () {
            // we save the new value
            register.set('contact', newValue);
            register.save(null, {
              useMasterKey: true, // we use the masterKey to pass the validation
              success: function () { response.success(); }
              error: function () { response.error(); }
            });
          });
        });
      },

      error: function(object, error) {
        response.error();
      }
    });
});

我们以updateContactCounter func:

结束
function updateContactCounter(contact, value, done) {
    var Counter = Parse.Object.extend('Counter');
    var query = new Parse.Query(Counter);

    // We get the right counter
    query.equalTo('name', 'register_contact_' + contact);
    query.first({
      success: function (obj) {
        obj.increment("count", value);
        obj.save(null, {
           success: function () {
             done();
           }
           error: function () {
             done();
           }
        });
      },
      error: function () {
        done();
      }
    });
}

你应该好...

这段代码只是让你理解这个过程,它肯定是完美也没有经过测试,它缺少重要的部分(错误和未定义的值处理),并且可以在很多方面得到改进(你可以使用promises并行执行updateContactCounter。