Updating the data fetching flow

Since we now know how to properly clean up Subscription, we can fix the main flow that we are using to fetch financial stock quotes.

First of all, let's make the MainActivity extend the RxAppCompatActivity so that access to the lifecycle methods will become available:

import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

public class
MainActivity extends RxAppCompatActivity {

The next step is to add the .bindToLifecycle() call with this:

.compose(bindToLifecycle())

So, the start of the flow will look like this:

Observable.interval(0, 5, TimeUnit.SECONDS)
.compose(bindToLifecycle())
.flatMap(
i -> yahooService.yqlQuery(query, env)
.toObservable()
)

This will make the Observable terminate whenever the MainActivity is destroyed. We can also move the entire flow to the onStart() method where it will be activated when a user opens an Activity and deactivated when it's closed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.43.26