Answer the question
In order to leave comments, you need to log in
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
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 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();
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 questionAsk a Question
731 491 924 answers to any question