如何检查Parse数据类Android中是否已存在值

时间:2020-12-27 04:28:26

I'm looking for a way on how can I check if a phone number exists already in Parse data class in Android.

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

For example, checking if a phone number exists already, if yes it returns true if not false.

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

I used this :

我用过这个:

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

and it doesn't help much.

它没有多大帮助。

3 个解决方案

#1


8  

Use getFirstInBackground() in the query and then simply check to see if there is a ParseException.OBJECT_NOT_FOUND exception. If there is, then the object doesn't exist, otherwise it is there! Using getFirstInBackground is better than findInBackground since getFirstInBackground only checks and returns 1 object, whereas findInBackground may have to query MANY objects.

在查询中使用getFirstInBackground(),然后只需检查是否存在ParseException.OBJECT_NOT_FOUND异常。如果有,那么该对象不存在,否则它就在那里!使用getFirstInBackground比findInBackground更好,因为getFirstInBackground只检查并返回1个对象,而findInBackground可能必须查询MANY对象。

Example

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


0  

Can you try something like this:

你能尝试这样的事吗:

    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
}

#3


0  

In general, these Parse callbacks pass you a ParseException and you can check the status code on the exception.

通常,这些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
       }
    }
}

There are a bunch of other more specific error code you can find out of getCode, here's the full list on Parse's docs. A few types of data have specific codes, for example when dealing with signup/login there are specific codes for EMAIL_NOT_FOUND.

您可以从getCode中找到许多其他更具体的错误代码,这里是Parse文档的完整列表。一些类型的数据具有特定代码,例如在处理注册/登录时,存在针对EMAIL_NOT_FOUND的特定代码。

#1


8  

Use getFirstInBackground() in the query and then simply check to see if there is a ParseException.OBJECT_NOT_FOUND exception. If there is, then the object doesn't exist, otherwise it is there! Using getFirstInBackground is better than findInBackground since getFirstInBackground only checks and returns 1 object, whereas findInBackground may have to query MANY objects.

在查询中使用getFirstInBackground(),然后只需检查是否存在ParseException.OBJECT_NOT_FOUND异常。如果有,那么该对象不存在,否则它就在那里!使用getFirstInBackground比findInBackground更好,因为getFirstInBackground只检查并返回1个对象,而findInBackground可能必须查询MANY对象。

Example

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


0  

Can you try something like this:

你能尝试这样的事吗:

    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
}

#3


0  

In general, these Parse callbacks pass you a ParseException and you can check the status code on the exception.

通常,这些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
       }
    }
}

There are a bunch of other more specific error code you can find out of getCode, here's the full list on Parse's docs. A few types of data have specific codes, for example when dealing with signup/login there are specific codes for EMAIL_NOT_FOUND.

您可以从getCode中找到许多其他更具体的错误代码,这里是Parse文档的完整列表。一些类型的数据具有特定代码,例如在处理注册/登录时,存在针对EMAIL_NOT_FOUND的特定代码。