Skip to content

Commit 0358361

Browse files
author
Kaushik Gopal
committed
fix: use concatEager vs concat courtesy Jake's suggestion
🙏 [Jedi Jake](https://twitter.com/JakeWharton/status/786362280535592961).
1 parent 4ead12d commit 0358361

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ The value of this technique becomes more apparent when you have more number of i
103103

104104
### Retrieve data first from a cache, then a network call - using [`.concat`](http://reactivex.io/documentation/operators/concat.html)
105105

106-
Using concat, you can retrieve information from an observable first (presumably this one is fast like retrieveing from a disk cache) and show preliminary data to a user. Subsequently, when the longer running 2nd observable is complete (say a network call), you can update the results on the interface using the latest information.
106+
Using concat, you can retrieve information from an observable first (presumably this one is fast like retrieving from a disk cache) and show preliminary data to a user. Subsequently, when the longer running 2nd observable is complete (say a network call), you can update the results on the interface using the latest information.
107107

108108
For the purposes of illustration i use an in-memory `List` (not an actual disk cache), then shoot out a real network call to the github api so it gives you a feel of how this can really be applied in production apps.
109109

110+
Note the use of `concatEager` here over the traditional `concat` operator. Both show the results from the `Observables` in a sequential manner (so disk first and then network). The `concat` operator however would not even begin the subscription on subsequent Observables unless the first one is complete whereas `concatEager` kicks off all Observables at the time of Subscription in parallel, but still preserves order.
111+
110112
**Update:**
111113

112114
After a [conversation I had with @artem_zin](https://twitter.com/kaushikgopal/status/591271805211451392), we arrived at an alternative solution to the same problem. One that used the [`.merge`](http://reactivex.io/documentation/operators/merge.html) operator instead.

app/src/main/java/com/morihacky/android/rxjava/fragments/PseudoCacheConcatFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void onDemoPseudoCacheClicked() {
5757
_resultList.setAdapter(_adapter);
5858
_initializeCache();
5959

60-
Observable.concat(_getCachedData(), _getFreshData())
60+
Observable.concatEager(_getCachedData(), _getFreshData())
6161
.subscribeOn(Schedulers.io())
6262
.observeOn(AndroidSchedulers.mainThread())
6363
.subscribe(new Subscriber<Contributor>() {

0 commit comments

Comments
 (0)