【问题标题】:How can i check if a value exists already in a Parse data class Android如何检查 Parse 数据类 Android 中是否已存在值
【发布时间】:2015-01-22 21:09:18
【问题描述】:

我正在寻找一种方法来检查电话号码是否已存在于 Android 的 Parse 数据类中。

例如,检查一个电话号码是否已经存在,如果是则返回 true,否则返回 true。

我用过这个:

query1.whereEqualTo("phone", "0644444444");
query1.findInBackground(new FindCallback<ParseObject>() {

这并没有多大帮助。

【问题讨论】:

    标签: java android database parse-platform


    【解决方案1】:

    在查询中使用 getFirstInBackground(),然后简单地检查是否存在 ParseException.OBJECT_NOT_FOUND 异常。如果存在,则该对象不存在,否则存在!使用 getFirstInBackground 比 findInBackground 更好,因为 getFirstInBackground 只检查并返回 1 个对象,而 findInBackground 可能需要查询许多对象。

    例子

    query1.whereEqualTo("phone", "0644444444");
    query1.getFirstInBackground(new GetCallback<ParseObject>() 
    {
      public void done(ParseObject object, ParseException e) 
      {
        if(e == null)
        {
         //object exists
        }
        else
        {
          if(e.getCode() == ParseException.OBJECT_NOT_FOUND)
          {
           //object doesn't exist
          }
          else
          {
          //unknown error, debug
          }
         }
      }
    });
    

    【讨论】:

      【解决方案2】:

      你能试试这样的吗:

          ParseQuery<ParseObject> query = ParseQuery.getQuery(PARSE_CLASS_NAME);// put name of your Parse class here
          query.whereEqualTo("phoneList", "0644444444");
          query.findInBackground(new FindCallback<ParseObject>() {
          public void done(List<ParseObject> phoneList, ParseException e) {
              if (e == null) {
                  handleIsUserNumberFound(! phoneList.isEmpty())
              } else {
                  Log.d("error while retrieving phone number", "Error: " + e.getMessage());
              }
          }
      });
      
      public class handleIsUserNumberFound(boolean isUserNumberFound){
          //do whatever you need to with the value
      }
      

      【讨论】:

      • 这不起作用,即使没有找到电话号码也永远不会显示错误,无论是否找到电话号码,ParseException e 始终为 null,如果有,则 e 不为 null查询有问题。
      【解决方案3】:

      通常,这些 Parse 回调会向您传递 ParseException,您可以检查异常的状态代码。

      query1.findInBackground(new FindCallback<ParseObject>() {
          public void done(List<ParseObject> objects, ParseException ex) {
             if(ex != null) {
                   final int statusCode = ex.getCode();
                   if (statusCode == ParsseException.OBJECT_NOT_FOUND) {
                        // Object did not exist on the parse backend
                   }
              }
              else {
                // No exception means the object exists
             }
          }
      }
      

      您可以从getCodehere's the full list on Parse's docs 中找到许多其他更具体的错误代码。少数类型的数据有特定的代码,例如在处理注册/登录时,EMAIL_NOT_FOUND 有特定的代码。

      【讨论】:

      • 我不太明白你的回答,你能举个例子,我在帖子中提到的案例吗?谢谢
      • 这是一个例子,如果对象不存在find回调会返回一个异常,其状态码包含OBJECT_NOT_FOUND。
      猜你喜欢
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 2021-10-10
      • 2017-03-09
      • 1970-01-01
      • 2011-05-16
      相关资源
      最近更新 更多