How to clean Asynctasks from memory after run?

Asked

Viewed 68 times

0

I am developing a commercial APP, in this need to load all customers of the establishment, so I make a request requesting each client, because I need to send the Id of this in the URL of the request, until there is well, the problem is that when I do for example, 150 client request works, this results in 150 new AsyncTask().execute();.

In a next scenario I need to order 700 customers, ie 700 new AsyncTask().execute() then I need to wipe from memory those that have already been completed, already tried new AsyncTask().executeOnExecutor(Executors.newSingleThreadExecutor()); and I was unsuccessful.


I have a ArrayList<Integer> that contains the ID of the clients that I need to 'pick up' (I have an Endpoint that returns the Client Ids that I can 'pick up'), so at each synchronization I send the 0 position of this array as parameter, then go the customer ID at the end remove the 0 position and make a check if there is still client without synchronizing.
Code of OnPostExecute():

clientesAguardandoSincronizar.remove(0);
mAsyncCliente = null;
if(clientesAguardandoSincronizar.size()>0){
     //Executa novamente o Async de Clientes para trazer um a um.
     mAsyncCliente = new AsyncCliente(context).execute();
}else{
     //Executa proxima AsyncTask.
}
  • Honestly, I think the biggest problem is with your API. Why not have an endpoint in the API that allows you to get all users at once? That way, you would always make a single request in a single AsyncTask.

  • 1

    @regmoraes I don’t know if this is the case, but not always the developer has control over the Apis he needs to consume.

  • How’s your code? How are these tasks created? Sharing the code you’ve already done makes it easy for others to understand your problem and help you.

  • On the first comment, the API is from another company that I need to integrate our app with this other guy’s business system. I have no control over it, even requested such correction and did not.

1 answer

0

If anyone has this problem, it happened to me because I was using Volley and every request he generated a Thread and left it in memory. I used Httprequest and I made all the requests of the same Asynctask, and it worked right.

  • Two answers? Why not one with edits? Or is information related to your question?

  • I’m new to the page, I’m not sure how it works. Instead of criticizing, because you didn’t come to help when you needed?

  • But thanks for the tip.

  • Youthful, I’m not criticizing you, I’m asking you, did you read what I wrote calmly? Two answers? Why not one with edits? Or is it information related to your question? Have you ever done the [tour]? It is highly recommended, so that problems like this do not occur.

  • These are information related to my question. The first is to complement and the last is to say what I did to work if anyone else needs.

  • So do the [tour], and also read this post to understand how the site works and the auto-response system. The way it is, it’s wrong and can be removed if you don’t fix it.

  • Okay. Thank you very much and I’m sorry if I was rude.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.