A Place where things start getting ugly

At first, it might look like a reasonable approach to extract and reuse code. However, it doesn't scale beyond one such extraction. Let's say that we want to extract another block that's responsible for the StockUpdate item's persistence and retrieval:

.doOnNext(this::saveStockUpdate)
.onExceptionResumeNext(StorIOFactory.createLocalDbStockUpdateRetrievalObservable(this))

Similarly, we will extract a method that will ensure that items are saved in the local database and later retrieved:

private Observable<StockUpdate> addLocalItemPersistenceHandling(Observable<StockUpdate> observable) {
return observable.doOnNext(this::saveStockUpdate)
.onExceptionResumeNext(StorIOFactory.createLocalDbStockUpdateRetrievalObservable(this));
}

However, now let's try applying that method to the flow:

addLocalItemPersistenceHandling(
addUiErrorHandling(
Observable.merge(
createFinancialStockUpdateObservable
(yahooService, query, env),
createTweetStockUpdateObservable
(configuration, trackingKeywords, filterQuery)
)
.compose(bindToLifecycle())
.subscribeOn(Schedulers.io())
.doOnError(ErrorHandler.get())
)
)
.doOnNext(update -> log(update))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(stockUpdate -> {
Log.d("APP", "New update " + stockUpdate.getStockSymbol());
noDataAvailableView.setVisibility(View.GONE);
stockDataAdapter.add(stockUpdate);
recyclerView.smoothScrollToPosition(0);
}, error -> {
if (stockDataAdapter.getItemCount() == 0) {
noDataAvailableView.setVisibility(View.VISIBLE);
}
});

We can see that the method had to wrap the existing call to the addUiErrorHandling():

Observable<StockUpdate> addLocalItemPersistenceHandling(Observable<StockUpdate> observable)

It is now obvious, that if we pursue this approach, we will end up with endless nested method calls that will make the refactoring and moving of code especially difficult.

Also, it will be really confusing--if we add addLocalItemPersistenceHandling() before addUiErrorHandling(), which operation will be applied first? We will end up with some really confusing and difficult-to-maintain code.

All of this can be done differently and in a much clearer fashion using ObservableTransformer interfaces and .compose() method calls on the Observable.

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

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