D
D
doq2016-04-06 22:37:57
Android
doq, 2016-04-06 22:37:57

Cons of using AsyncTask?

Hello! I'm new to Android development, so please do not judge strictly) I studied in courses and in most cases we used Asynctask for asynchronous calls. Now, when applying for a job, I am often asked the following question - "Why can't I use Async-Tasks and what should I use instead?". I would like to clarify the situation. First of all, I would like to ask experienced developers what they use? What should I study right now? Thanks to all)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis Zagaevsky, 2016-04-06
@zagayevskiy

Why not? Can! But not always :)
AsyncTasks - for short asynchronous operations. A typical example is accessing a database. Under no circumstances should it be used for networking.
Why:
1) AsyncTasks are not tied to the activity life cycle. When the activity is recreated, onPostExecute() will be executed, but the activity will not be the same (and it may leak, which is not cool at all).
2) Corollary 1 - you need to get confused pretty well in order to write the correct code with AsyncTasks. Such code becomes inflexible and inconvenient.
3) Without additional effort (executeOnExecutor), asynctasks are queued and executed in a separate thread, but sequentially. As a result, UI brakes where they are used correctly (for example, AsyncTaskLoader).

A
Aleksandr, 2016-04-15
@alvlapo

It is described in detail in this article , do not add, do not subtract.

A
Alexander Vasilenko, 2016-04-07
@SanchelliosProg

The downside is that if you execute a large request in AsyncTask, then if you switch to another Activity, for example, the GC may not destroy the Activity if the task has not completed. This leads to memory leaks, which is bad. For example, when working with a network, it is better to use EventBus (greenrobot), which starts its work in a separate thread. So it goes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question