我如何检查解析数据中是否已存在值 class Android

How can i check if a value exists already in a Parse data class Android

我正在寻找一种方法来检查 phone 号码是否已经存在于 Parse 数据 class in Android.[=14= 中]

例如,检查 phone 号码是否已经存在,如果存在则 returns 如果不存在则为真。

我用过这个:

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

也没什么用。

通常,这些 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 中找到许多其他更具体的错误代码。一些类型的数据有特定的代码,例如在处理 signup/login 时有 EMAIL_NOT_FOUND.

的特定代码

你能试试这样吗:

    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
}

在查询中使用 getFirstInBackground(),然后简单地检查是否有 ParseException.OBJECT_NOT_FOUND 异常。如果存在,则该对象不存在,否则存在!使用 getFirstInBackground 优于 findInBackground,因为 getFirstInBackground 仅检查 returns 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
      }
     }
  }
});