talklittle / reddit-is-fun

OLD VERSION 1.3 of reddit is fun -- Android app to interact with reddit.com
GNU General Public License v3.0
372 stars 157 forks source link

replace AsyncTask with AsyncTaskLoader #280

Open yulin2 opened 9 years ago

yulin2 commented 9 years ago

Hello, I'm doing research on Android async programming. Some articles (for example this article) mention that AsyncTask leads to memory leak and losing task result when there's a configuration change (such as orientation change). Android docs recommend AsyncTaskLoader (require API level 11), which avoid the problems in AsyncTask.

I try to replace one AsyncTask with AsyncTaskLoader in reddit-is-fun in this pr (you don't have to merge). Do you think AsyncTaskLoader will work better for reddit-is-fun? Do you want to replace all AsyncTask to AsyncTaskLoader?

talklittle commented 9 years ago

Yes, AsyncTaskLoader is better when working with Fragments. These days I prefer the Loader framework over AsyncTask whenever possible.

However, the most recent fad is RxJava. Since you are doing research, you should look into how Loaders compare with Reactive event processing. Some people eschew Loaders (and even Fragments) entirely, in favor of simpler mechanisms.

yulin2 commented 9 years ago

Yes, RxAndroid(or RxJava) supports emit multiple values and it provides better way to issue exceptions. However, from my knowledge to Rx, you still need to handle its lifecycle manually to avoid memory leak (for example, unsubscribe all Observers in onDestroy). Also, RxAndroid doesn't prevent losing task result. This drawback is similar to AsyncTask.

For Loader, its lifecycle is handled by Android system automatically to avoid memory leak, and it caches the task result so the task needn't to be executed repeatedly if there's an orientation change. From this point of view, I think AsyncTaskLoader is better. What do you think?