How to cancel query operaiton? - Android

This is an example query.

ParseQuery getUser = new ParseQuery(ParseUser.class);
getUser.whereEqualTo(“username”,usernameString);
getUser.getFirstInBackground(new GetCallback() {
@Override
public void done(ParseUser object, ParseException e) {

    }
});

To aviod memory leaks I wanna cancel this operation. How can I do that?

What do you mean by cancel the operation? Before the done callback has been called, do you want the query to stop running? It is not possible. Right after you call getUser.getFirstInBackground, a http request is sent to the Parse Server and it starts executing the query. There is no way to stop this process.

Someone did tell me this. https://parseplatform.org/Parse-SDK-Android/api/com/parse/ParseQuery.html#cancel--

So this “cancel” method dont work?

This method cancels the task in the client but the query will continue running on the server. I am not sure if it is helpful for what you need to achieve.

1 Like

I want to cancel AsyncTask to avoid any furter memory leaks in my app. Based on your description cancel method is what I’m loking for

1 Like

I cancel query with cancel method in Ondestroy method of activity. But done method still called.

ParseQuery getUser = new ParseQuery(ParseUser.class);
getUser.whereEqualTo(“username”,usernameString);
getUser.getFirstInBackground(new GetCallback() {
    @Override
    public void done(ParseUser object, ParseException e) {
        Log.e("cancel test","canceltest" );
        //This log shouldnt be exist but I can see it after activity destroyed.
    }
});


@Override
protected void onDestroy() {
    getUser.cancel();
    super.onDestroy();

}

Is this expected behaviour?

Yes. Then you can check in the e var if it was cancelled before doing any further action.

What is the error code I get when I cancel query? I checked the api docs and I found OPERATION_FORBIDDEN But I’m not sure. Is this the right error code?

It is actually not clear to me but I reviewed the SDK code and it seems it will return object = null and e = null. Do you mind to debug and share what are the values of these vars when cancelling the query?

Yes. I will test and share results here.

1 Like

Seems like cancel method doesn’t work. I start query and cancel right after it starts like this

getUser.getFirstInBackground(new GetCallback<ParseUser>() {
        @Override
        public void done(ParseUser object, ParseException e) {
            //Cancel method runs before this method
                   
            if(object==null){
                Log.e("cancel query","Object null");
            }
            if(object!=null){
                Log.e("cancel query","Object not null");

            }


            if(e==null){
                Log.e("cancel query","error null");
               //do other tasks
            }
            if(e!=null){
                 Log.e("cancel query","Error not null. Error code: "+String.valueOf(e.getCode()));
            }
        }
    });
    getUser.cancel();

Since getfirstinbackground method is async task, cancel method runs before done method. I check both exception object and result object. seems like query didnt stop and countinue working like nothing happend. Error object was null and result object retrieved succesfully

I’ve just seen in the docs that this method is actually deprecated: https://github.com/parse-community/Parse-SDK-Android/blob/master/parse/src/main/java/com/parse/ParseQuery.java#L246

You need to use the cancellation token system instead. It is not well documented though. You can find some information in the Bolts repo: https://github.com/BoltsFramework/Bolts-Android#cancelling-tasks

1 Like

That’s bad. Bolts documents are also not good. I couldn’t understand what to do. I will open an issue on GitHub. I guess your main experience on parse is JavaScript sdk?

Yes… my main experience is with JavaScript SDK.