GeezerVison is showing github.com — feat: wip: write pseudo cache example using .concat · daquino/Android-RxJava@736b1ac
File treeExpand file treeCollapse file tree Open diff view settings java/com/morihacky/android/rxjava
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +8-3Lines changed: 8 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -72,7 +72,7 @@ Have a look at the accompanying blog posts for details on this demo: | 72 | 72 | 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/) | 73 | 73 | 3. [share/publish/refcount](http://nerds.weddingpartyapp.com/tech/2014/12/24/rxjava-share-publish-refcount-and-all-that-jazz/) | 74 | 74 |
| 75 | | -### Form validation using combineLatest | | 75 | +### Form validation - using [`.combineLatest`](http://reactivex.io/documentation/operators/combinelatest.html) | 76 | 76 |
| 77 | 77 | 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). | 78 | 78 |
| @@ -85,12 +85,17 @@ Note that the `Func3` function that checks for validity, kicks in only after ALL | 85 | 85 | 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). | 86 | 86 |
| 87 | 87 |
| | 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 | + | 88 | 95 | ## Work in Progress: | 89 | 96 |
| 90 | 97 | Examples that I would like to have here, but haven't found the time yet to flush out. | 91 | 98 |
| 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) | 94 | 99 |
| 95 | 100 | ### Pagination (wip) | 96 | 101 |
| |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/MainActivity.java +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,7 +20,8 @@ protected void onCreate(Bundle savedInstanceState) { | 20 | 20 |
| 21 | 21 | getSupportFragmentManager().beginTransaction() | 22 | 22 | .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()) | 24 | 25 | .commit(); | 25 | 26 | } | 26 | 27 |
| |
Collapse file Copy file name to clipboard +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
Collapse file Copy file name to clipboard +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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> |
|