Skip to content

Commit 736b1ac

Browse files
author
Kaushik Gopal
committed
feat: wip: write pseudo cache example using .concat
1 parent d582883 commit 736b1ac

4 files changed

Lines changed: 51 additions & 4 deletions

File tree

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Have a look at the accompanying blog posts for details on this demo:
7272
2. [DebouncedBuffer used for the fancier variant of the demo](http://nerds.weddingpartyapp.com/tech/2014/12/24/secret-bonus-part-debouncedbuffer-used-in-rxbus-example/)
7373
3. [share/publish/refcount](http://nerds.weddingpartyapp.com/tech/2014/12/24/rxjava-share-publish-refcount-and-all-that-jazz/)
7474

75-
### Form validation using combineLatest
75+
### Form validation - using [`.combineLatest`](http://reactivex.io/documentation/operators/combinelatest.html)
7676

7777
Thanks to Dan Lew for giving me this idea in the [fragmented podcast - episode #5](http://fragmentedpodcast.com/episodes/4/) (around the 4:30 mark).
7878

@@ -85,12 +85,17 @@ Note that the `Func3` function that checks for validity, kicks in only after ALL
8585
The value of this technique becomes more apparent when you have more number of input fields in a form. Handling it otherwise with a bunch of booleans makes the code cluttered and kind of difficult to follow. But using `.combineLatest` all that logic is concentrated in a nice compact block of code (I still use booleans but that was to make the example more readable).
8686

8787

88+
### Retrieve data first from a (pseudo) cache, then a network call - using [`.concat`](http://reactivex.io/documentation/operators/concat.html)
89+
90+
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.
91+
92+
For the purposes of illustration i use an in-memory `List` (not an actual disk cache), but 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.
93+
94+
8895
## Work in Progress:
8996

9097
Examples that I would like to have here, but haven't found the time yet to flush out.
9198

92-
### First retrieve from cached data, if no cache found make a network call if you can't find your data (concat) (wip)
93-
[Courtesy: gist](https://gist.github.com/adelnizamutdinov/7483969)
9499

95100
### Pagination (wip)
96101

app/src/main/java/com/morihacky/android/rxjava/MainActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ protected void onCreate(Bundle savedInstanceState) {
2020

2121
getSupportFragmentManager().beginTransaction()
2222
.addToBackStack(this.toString())
23-
.replace(R.id.activity_main, new MainFragment(), this.toString())
23+
//.replace(R.id.activity_main, new MainFragment(), this.toString())
24+
.replace(R.id.activity_main, new PseudoCacheConcatFragment(), this.toString())
2425
.commit();
2526
}
2627

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.morihacky.android.rxjava;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.support.v4.app.Fragment;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import butterknife.ButterKnife;
10+
import com.morihacky.android.rxjava.app.R;
11+
import rx.Subscription;
12+
13+
public class PseudoCacheConcatFragment
14+
extends Fragment {
15+
16+
private Subscription _subscription = null;
17+
18+
@Override
19+
public View onCreateView(LayoutInflater inflater,
20+
@Nullable ViewGroup container,
21+
@Nullable Bundle savedInstanceState) {
22+
View layout = inflater.inflate(R.layout.fragment_form_validation_comb_latest, container, false);
23+
ButterKnife.inject(this, layout);
24+
return layout;
25+
}
26+
27+
@Override
28+
public void onPause() {
29+
super.onPause();
30+
if (_subscription != null) {
31+
_subscription.unsubscribe();
32+
}
33+
}
34+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<LinearLayout
2+
android:orientation="vertical"
3+
android:layout_height="match_parent"
4+
android:layout_width="match_parent"
5+
xmlns:android="http://schemas.android.com/apk/res/android"
6+
>
7+
</LinearLayout>

0 commit comments

Comments
 (0)