0

I'm using the Parse API to query some data for an Android application and I would like to know how to return a value (for example a Boolean) from a Parse Query. For example I would like a function that returns true if some data exists and false otherwise like so :

    public Boolean myFunction(){
      ParseQuery<ParseObject> query = ParseQuery.getQuery();
      query.findInBackground("someData",new GetCallback<ParseObject>() {
        @Override
        public void done(ParseObject lan, ParseException e) {
            if(e==null){
              return true;
            } else {
              return false;
            }
        });
    }

I do know that this cannot be done this way because the query is processed in a background thread and I'm not very familiar with Callbacks. I am aware that there is a similar question here Parse.com how to get return value of query but this is for JavaScript. Do you have any idea on how to do that ?

2
  • 1
    You could explore looking at a bit of cloud code that just returns your boolean result. This should save having to download the data locally. docs.parseplatform.org/cloudcode/guide Commented Apr 25, 2017 at 8:37
  • @buckettt thanks I will look into that. Commented Apr 25, 2017 at 11:42

2 Answers 2

0

You are almost there. When you get the Parse Object extract it with:

boolean myBoolean = myParseObject.getBoolean("myBooleanColumn");

Full example (finding an object via id, it can be adapted for other type of queries):

ParseQuery<ParseObject> query = ParseQuery.getQuery("YourClass");
query.getInBackground("id", new GetCallback<ParseObject>() {
  public void done(ParseObject myParseObject, ParseException e) {
    if (e == null) {
      boolean myBoolean = myParseObject.getBoolean("myBooleanColumn");
    } else {
      // something went wrong
    }
  }
});

Update: if you only want to check if some data exists in a row you can do it with

query.whereEqualTo("columnToFind", "searchterm");

You can even find compare an array with the data in row with

query.whereContainsAll("columnToFind", arrayOfThingsToSearch);
Sign up to request clarification or add additional context in comments.

1 Comment

That's not exactly what I want to do. I don't want to get data from a parse object but check if some data exists in some row. Then I could use if(!myFunction) then do something
0

After some research and thanks to @buckettt, the easiest way to accomplish that is to use Parse Cloud Code. Define your function in the main.js file inside parse-server folder :

Parse.Cloud.define("myFunction",function(req,res){
   var userId = req.params.userId; //params passed in Client code
   var myQuery = new Parse.Query(Parse.User);
   myQuery.equalTo("userId", userId);
   myQuery.find({
      success: function(results) {
          res.success(results.get("userName"));
      }
      error: function() {
         res.error("Failed !");
      }
   }

});

And in your Client's code :

    HashMap<String, Object> params = new HashMap<String, Object>();
    params.put("userId",userId);
    ParseCloud.callFunctionInBackground("myFunction", params, new FunctionCallback<String>() {
        public void done(String res,ParseException e){
            if (e == null) {
                Log.i("Results :",res);
            } else {
                Log.i("Error",e.getMessage());
            }
        }
    });

This way you return the desired value and the function is executed directly on your server. Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.