Search code examples
androidparse-platform

Can you pull an answer out of a parse query?


I am trying to make an app but I am running into issues of not being able to pull information out of a query.

My code looks something like this:

// This is to validate there is an email    
private boolean isEmail(String email) {

      ParseQuery<ParseUser> query = ParseUser.getQuery();
      query.whereEqualTo("email", email);
      query.getFirstInBackground(new GetCallback<ParseUser>() {
          @Override
          public void done(ParseUser object, ParseException e) {
              if (object == null) {
                  Log.d("Login User", "The getFirst request failed. Probably because no associated account found");
                  return false
              }
              else {
                  return true
              }
          }
      });
}

But as you can see the places that have the "return" statement would spit out errors due to where they are being called out of.

Is there some way to do the return statment?


Solution

  • You can use query.getFirst instead of query.getFirstInBackground. Note that it will block the main thread. You probably want to redesign your function to receive a callback function that will be called back with the result. In the case you really want to keep the signature, that would be the way to go:

    private boolean isEmail(String email) {
        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.whereEqualTo("email", email);
        try {
            ParseUser object = query.getFirst();
            return true;
        } catch (ParseException e) {
            return false;
        }
    }
    

    Also note that using this method you will be able to only retrieve the current user, since master key is required to return the other users.