Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Android Loaders

Loaders

  1. monitor data source and deliver new results
  2. After a configuration change : no need to re-query the data

I read the android guide about Loaders. I read Alex Lockwood 4 parts tutorial . Tested his sample app too. Tried to read the Google App for I/O 13, there's a Stream feature and reading its code find out it uses Loaders since it provides code to create a StreamLoader. Here is the Link I suppose they use it to monitor for new data and add them to their view.

Same for Alex's app. There's an observer and when there is new data entries triggers a refresh for the UI.

So far it seems to me, Loaders are ideal choice for a "livescore" app. When there's a new update ( which means a new data entry ) it appears on your screen.

Maybe something like Twitter. New messages for you, custom Observer to notice for changes, custom Loader brings the data and an adapter to display them. No need to "pull-to-refresh". But then again Twitter has its own RESTful API which kinda does the same job. No need for a pointer to the new data. ( don't know how they do it but I guess somehow the "push" new data to your device ).

So my question is :

Loaders are best option when we want to observe a data source and change our view so it will display the new data?

Are there any examples/app I can check dealing with that logic : monitor the data source -> get the data -> refresh UI

Any characteristic cases ( like the one with the "livescore" previously mentioned by me ) that when we have to deal with them we have to choose Loaders?

The second part of the Loaders ( configuration change, keeping the data ) I think its clear. No one want's to re-download an Image gallery when the user rotates the device.

Thank you and excuse my confusion

like image 648
test2558 Avatar asked Aug 09 '13 05:08

test2558


People also ask

What are Android loaders?

Loaders persist and cache results across configuration changes to prevent duplicate queries. Loaders can implement an observer to monitor for changes in the underlying data source. For example, CursorLoader automatically registers a ContentObserver to trigger a reload when data changes.


1 Answers

The best way I can describe a Loader is a Handler that is always on. Both Loaders and Handlers pass data between objects.

I agree with what you said about the "livescore" app. The Loader monitors the source of their data and delivers new results when the content changes.

To answer your questions:

1) Loaders are best option when we want to observe a data source and change our view so it will display the new data?

A: Yes. if your data source is constantly updating. For example, like a stock-ticker app. If your data isn't constantly updating, then no, don't use a loader. For example, if your data source is only retrieved once, then there's no need for a Loader.

2) Are there any examples/app I can check dealing with that logic : monitor the data source -> get the data -> refresh UI

A: https://www.youtube.com/watch?v=3d9BeWqlfTk

like image 198
Gene Avatar answered Oct 02 '22 22:10

Gene