简体   繁体   中英

Parse.com How to return a value from a query in Android

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 ?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM