So what to use when doing job outside main thread? Plain old Java thread? AsyncTask? Loader? Maybe something catchy, like RoboSpice or even rxAndroid?
Let’s dive deeply into nearly all possible ways of implementing async operations on Android. Find pros and cons, share our experience with all attitudes.
8. StrictMode
public class DemoApp extends Application {
@Override
public void onCreate() {
super.onCreate();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());
}
}
9. "AsyncTask enables proper and easy use of
the UI thread. This class allows to perform
background operations and publish results on
the UI thread without having to manipulate
threads and/or handlers.”
Android SDK docs
16. AsyncTask
Pros
● Simple
● Great for few seconds
operations
● Cancellation support
Cons
● Memory leaks,
● Possible crashes
● Behaves different
● Lack of exceptions handling
(or RoboAsyncTask)
http://stackoverflow.com/questions/3357477/is-asynctask-really-conceptually-flawed-or-am-i-just-
missing-something
17. Adam wonders: Really a good idea?
run task
→ change config (e.g. by rotating the screen)
→ cancel previous task
→ run new task
→ update UI
19. Loaders, in particular CursorLoader, are
expected to retain their data after being
stopped. This allows applications to keep
their data across the activity or fragment's
onStop() and onStart() methods, so that when
users return to an application, they don't
have to wait for the data to reload.
Android SDK docs
20. AsyncTaskLoader
“This class performs the same function as
the AsyncTask, but a bit better. It can
handle Activity configuration changes more
easily (add MG: by Loader Manager), and it
behaves within the life cycles of Fragments
and Activities.”
http://www.javacodegeeks.com/2013/01/android-loaders-versus-asynctask.html
21. How it works...
● LoaderManager
● initLoader
● LoaderCallback
● CursorLoader
● AsyncTaskLoader
32. Loader
Pros
● Better than AsyncTask, much
● Gives “partial” caching
○ Need in-loader result
caching when leaving
activity
○ No control
● No memory leaks
Cons
● Designed to support DB data
(cursor)
● Hacking, hacking, hacking to
use in networking
● Poorly documented
● Opposite to AsyncTask
○ too highly coupled
● Poor exception management
34. The idea
“[...]
Basically, what happens with RS is that when a request is being
processed, its listeners will be invoked as long as the associated
activity is alive.
[...]
The main purpose of RS is to make sure that there is no memory leak :
your activity, if it has to die, will die and be garbage collected, RS
doesn't hold any hard reference to it that would prevent garbage
collection. That's really the core idea behind RoboSpice. [...]”
Stephen Nicolas
http://stackoverflow.com/questions/19011200/how-does-robospice-manage-activity-lifecycle
41. By default
● Handling config changes
● Full control caching
● Multithreading
● No internet connection → NoNetworkException.class
● Modules to SpringAndroid, Retrofit
● and much more...
42. RoboSpice
Pros
● Robust, full featured
● Last commit < 3 months ago
Cons
● POJO per Request
● No a simple library
○ SpiceManager with 1300
LOC
43. Go Reactive
● ReactiveExtentions by MS
● Ported by Netflix
● rxAndroid = rxJava + few Android classes
● rxJava hit 1.0
● rxAndroid still 0.x
45. Motivations
"[...] If a calling Activity/Fragment makes multiple
requests, it needs to handle responses in a single function
by switching on the URL. This makes for code that isn’t very
readable, especially when you have to jump from where the
request is executed to where the response is handled"
http://markhudnall.com/2013/10/15/rxjava-and-android/
46. Motivations
“It’s still a little verbose, but it’s more
similar to writing synchronous code. I use
the response in code that directly follows
the request. I also get error handling on
the UI thread for free.”
http://markhudnall.com/2013/10/15/rxjava-and-android/
50. Worth trying?
● Streams of events
○ e.g. Weather station, tweets analyzer
● In “common” app?
● When using Retrofit
@GET("/user/{id}/photo")
Observable<Photo> getUserPhoto(@Path("id") int id);
51. rxAndroid
Pros
● Reduced callback hell with
FPP
● Robust set of
functions/transformations
Cons
● Need to change the way of
thinking
● Hard, really
● On Android - even harder
● Possible memory leaks?
● Debugging?
52. Out of scope… or time
● IntentService (!= Service)
● Event buses (Otto, GreenRobot’s EventBus)
○ especially with Loaders
● Volley
● Ion
53. Remember
● Don’t use AsyncTask
● Consider using Loaders
● Give a try RoboSpice
● Learn FRP (rxJava/rxAndroid)