P
P
postflow2016-05-04 11:18:37
Android
postflow, 2016-05-04 11:18:37

Asynctask or loader for network requests?

What, in terms of application architecture, should be used to access the server? asynctask or loader ?
here (in the comments) , experienced developers scold asyncTask for network requests.
For which tasks would asyncTask be more attractive?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ilya Pavlovsky, 2016-05-04
@TranE91

Retrofit + RxJava = scalability, convenience in support and other goodies. As for me, this is the best spark to date for working in REST.
And about AsyncTask, this stray seems to be already outdated, maybe it will be marked and mowed down soon.

I
IceJOKER, 2016-05-04
@IceJOKER

I have been working with AsyncTask for several years now and have not seen any problems, at the beginning of the journey there were small problems due to the fact that the Activity was shutting down, and the background task was running and after the execution it crashed the app, but these were problems with crooked hands.
...
To communicate with the activity, I use listeners, and in extreme cases, you can create it as an inner class (which I don’t play around with, for me it’s better to have a separate class), for manipulation I assign a variable at the activity (fragment) level and when onStart / occurs onStop I stop / start the task, and there are no problems with the life cycle of the host (Activity, Fragment ...)
ps you need to remember that after one start of the task, you need to re-create it, so keep this in mind when creating it:
private AsyncTask task;
....

if(task == null || task.getStatus() == AsyncTask.STATUS_FINISHED) //можно/учитывать учитывать и другие состояния - PENDING, RUNNING.. это уже зависит от задачи
{
  task = new....;
}
task.execute();

well, or you don’t need a response, but you just need to perform some task (file upload, etc.), then just start the task and forget about it

R
Rustem Saitkulov, 2016-05-12
@atetc

The main disadvantages of AsyncTasks:
- memory leak (stores a reference to the context)
- the API does not provide for error handling
- there is no binding to the life cycle
- work with only one thread that cannot be customized
Can be used, but only for simple short operations and very carefully
RxJava + RxAndroid has been recommended as a worthy alternative for more than a year

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question