https://kotlinlang.org logo
Title
d

dragas

05/17/2017, 11:41 AM
I have a silly question. Various questions on SO and tutorials on the web suggest saving a listener interface in your
AsyncTask
to still keep a reference and publish progress to your fragment/activity/service/anything. Question stands, shouldn't it be
WeakReference
? I'd assume that ART does not garbage collect running threads and if your application is suddenly ended, keeping a hard reference to your context will prevent ART from garbage collecting it.
m

miha-x64

05/17/2017, 12:41 PM
dragas: if your application have suddenly ended, ART VM will be just killed with whole its heap. But if your UI is being killed due to screen rotation, you will get short-time memory leak, so yes, reference should be weak, and AsynkTask — static (if nested). Also, AsynkTasks are unaware of UI lifecycle, so it may be better to use Loaders or MVP.
k

kirillrakhman

05/17/2017, 1:21 PM
or just don't use
AsyncTask
as there are much better alternatives
a

abdellah

05/17/2017, 1:46 PM
Nobody use
AsyncTask
those days because their limitations/problems
m

miha-x64

05/17/2017, 1:49 PM
@abdellah wrong, I use `AsyncTaskLoader`s! :troll:
👻 1
a

abdellah

05/17/2017, 1:51 PM
nice to meet you 🆘
😅 1
d

dragas

05/17/2017, 1:52 PM
@kirillrakhman I'd assume a better solution would be using Anko's
doAsync
?
k

kirillrakhman

05/17/2017, 2:55 PM
doAsync is just a wrapper around
AsyncTask
and only mitigates the problem with leaks by keeping weak references. Using RxJava in combination with, say RxLifecycle will give you a clearer code structure and better leak protection