Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

I used this :

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

and it doesn't help much.

like image 321
Stranger B. Avatar asked Jan 10 '23 01:01

Stranger B.


1 Answers

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.

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
      }
     }
  }
});
like image 104
SavageKing Avatar answered Jan 24 '23 09:01

SavageKing