Java – How do I check if a value already exists in the Parse data class Android

How do I check if a value already exists in the Parse data class Android… here is a solution to the problem.

How do I check if a value already exists in the Parse data class Android

I’m looking for a way to check if a phone number already exists in Android’s Parse data class.

For example, check if a phone number already exists, return true if it exists, otherwise return false.

I’ve used this :

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

It didn’t help much.

Solution

Use getFirstInBackground() in the query and then simply check for ParseException.OBJECT_NOT_FOUND exceptions. If it exists, the object does not exist, otherwise it exists! Using getFirstInBackground is preferable to findInBackground because getFirstInBackground only checks and returns 1 object, whereas findInBackground may have to query many objects.

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
      }
     }
  }
});

Related Problems and Solutions