Skip to content

Data Flow

Phil Lopreiato edited this page Dec 15, 2015 · 2 revisions

This page covers how we put data into a Fragment. The process splits up parsing and rendering data so they can happen at different times and on different threads.

Getting Data

A fragment that wants to easily tap into the app's datafeed should extend DatafeedFragment (ListViewFragment and ExpandableListViewFragment are also available to simplify the code even further for list-based fragments). DatafeedFragment has 4 generic types: The datatype returned by the Observable, the datatype that gets sent to the UI, a Subscriber that transforms the former type into the latter, and a binder that knows how to draw the UI from the second type. Each fragment needs to implement #getObservable. This method supplies a new Observable that passes through the data it'll need. Typically, this is done by calling into CacheableDatafeed and using one of the existing API calls.

Subscribing to Data

Every DatafeedFragment needs to have a "Subscriber". This is a class that is passed an item from the Observable chain and transforms it into something "Bindable". Subscribers need to extend BaseAPISubscriber by implementing the method parseData.

Subscribers execute on a background thread and are fired off as soon as data is available.

Binding Data

Finally, each DatafeedFragment needs to have a "Binder". This is a class that takes in the result from the Subscriber and updates the UI. Binders can also use ButterKnife to obtain references to Views (from a root view passed to the Binder in Fragment#onCreateView). Binders need to extend AbstractDataBinder and most importantly implement updateData, the callback to manipulate Views.

Binders update data on the UI thread and will be called on-demand. For example, in a ViewPager, we will only bind tabs that are adjacent to the current tab. We don't need to bind views offscreen - it would just add unnecessary work to the UI thread.

Clone this wiki locally