Play with this demo
In this Activity, you can :
- start a Loader observe the result : getting a list of tweets about android ;
- cancel a started Loader ;
- start a Loader, rotate the device and observe that UI is updated as expected (result is cached by Loaders);
- after you rotated the device, the loader is still complete : starting it again will get you instantly with the
result (cached result is returned) and you must cancel the loader to update the tweets.
- rotate the device as much as you want, even during loading, loaders are robust;
- observe no more memory leak, you can start as many Loaders as you want.
Loader for REST request.
This Activity uses a Loader to get the list of tweets related to Android.
a Proof of Concepts
It is based on an idea developed by Neil Goodmann in its article :
Modern Techniques For Implementing REST Clients On Android 4.0 And below
(part 1 and
part 2). Neil Goodmann
wrote a complete tutorial and made the source code available on a Git Hub repo.
We look forward to re-implement the loader using a more standard library for executing rest requests :
the spring android RestTemlate, but current RestLoader proposed by Neil Goodmann is mature enough
for this demo.
Although the technique doesn't seem to be very well know from Android developers, it's actually a neat solution : it works perfectly and
offers a robust approach to asynchronous job execution, without any kind of memory issue.
Progress and loaders
On this demo activity, you can't observe the progress of the Loader's job. But we believe this should be possible.
Cancelling the request
Loaders can be cancelled easily and this applies to this example as well.
Note that, once REST request is sent, it can't be aborted. We believe this limitation could be leveraged as well.
Networking operations
Nevertheless, when it comes to networking operations, using Loaders has several drawbacks and limitations that we will summarize as follow :
- Loaders handle Activity life cycle pretty well. If you start the request, get the result and rotate the device, you still get the result displayed but
- If you click on the start button and rotate the device before getting the result then the request is executed but previous response is lost, cache
is cleared before restarting the loader.
- Moreover the result is not stored on disk. So, if you leave this activity and come back, you will have lost the result of the REST request.
- If you click twice on the start button, the second click has no effect. If you want to refresh the results, you must first cancel the Loader
and then start it again. This life cycle is rather counterintuitive and illustrates how far Loaders' design is from common networking requirements.
We now propose a new approach for asynchronous network operations, using RoboSpice. Next tutorial is the exact same application, but spiced. As you will
see RoboSpice allows to go far beyond those limitations.