@@ -15,14 +15,6 @@ The long operation is simulated by a blocking Thread.sleep call (since this is d |
15 | 15 |
|
16 | 16 | To really see this example shine. Hit the button multiple times and see how the button click (which is a ui operation) is never blocked because the long operation only runs in the background. |
17 | 17 |
|
18 | | -### Instant/Auto searching (subject + debounce) |
19 | | - |
20 | | -This is a demo of how events can be swallowed in a way that only the last one is respected. A typical example of this is instant search result boxes. As you type the word "Bruce Lee", you don't want to execute searches for B, Br, Bru, Bruce, Bruce , Bruce L ... etc. But rather intelligently wait for a couple of moments, make sure the user has finished typing the whole word, and then shoot out a single call for "Bruce Lee". |
21 | | - |
22 | | -As you type in the input box, it will not shoot out log messages at every single input character change, but rather only pick the lastly emitted event (i.e. input) and log that. |
23 | | - |
24 | | -This is the debounce/throttleWithTimeout method in RxJava. |
25 | | - |
26 | 18 | ### Accumulate calls (buffer) |
27 | 19 |
|
28 | 20 | This is a demo of how events can be accumulated using the "buffer" operation. |
@@ -31,45 +23,54 @@ A button is provided and we accumulate the number of clicks on that button, over |
31 | 23 |
|
32 | 24 | If you hit the button once. you'll get message saying the button was hit once. If you hit it 5 times continuosly within a span of 2 seconds, then you get a single log, saying you hit that button 5 times (vs 5 individual logs saying "Button hit once"). |
33 | 25 |
|
34 | | -Two implementations: |
| 26 | +Two possible implementations: |
35 | 27 |
|
36 | | -2a. Using a traditional observable - but encompassing the OnClick within the observable |
| 28 | +2a. Using a traditional observable - but encompassing the OnClick within the observable (as demoed here) |
37 | 29 | 2b. Using PublishSubject and sending single clicks to the Observable, which in-turn then sends it to the Observer |
38 | 30 |
|
| 31 | +### Instant/Auto searching (subject + debounce) |
| 32 | + |
| 33 | +This is a demo of how events can be swallowed in a way that only the last one is respected. A typical example of this is instant search result boxes. As you type the word "Bruce Lee", you don't want to execute searches for B, Br, Bru, Bruce, Bruce , Bruce L ... etc. But rather intelligently wait for a couple of moments, make sure the user has finished typing the whole word, and then shoot out a single call for "Bruce Lee". |
| 34 | + |
| 35 | +As you type in the input box, it will not shoot out log messages at every single input character change, but rather only pick the lastly emitted event (i.e. input) and log that. |
| 36 | + |
| 37 | +This is the debounce/throttleWithTimeout method in RxJava. |
| 38 | + |
39 | 39 | ### Retrofit and RxJava (zip, flatmap) |
40 | 40 |
|
41 | | -[Retrofit from Square](http://square.github.io/retrofit/) is an another amazing library that helps with easy networking (even if you haven't made the jump to RxJava just yet, you really should check it out). It works even better with RxJava and these are examples taken straight up from the android demigod developer Jake Wharton's talk at Netflix. You can [watch the talk](https://www.youtube.com/watch?v=aEuNBk1b5OE#t=2480) at this link. Incidentally, my motiviation to use RxJava was from attending this talk at Netflix. |
| 41 | +[Retrofit from Square](http://square.github.io/retrofit/) is an amazing library that helps with easy networking (even if you haven't made the jump to RxJava just yet, you really should check it out). It works even better with RxJava and these are examples hitting the github api, taken straight up from the android demigod-developer Jake Wharton's talk at Netflix. You can [watch the talk](https://www.youtube.com/watch?v=aEuNBk1b5OE#t=2480) at this link. Incidentally, my motiviation to use RxJava was from attending this talk at Netflix. |
42 | 42 |
|
43 | | -Since it was a presentation, Jake only put up the most important code snippets in [his slides](https://speakerdeck.com/jakewharton/2014-1). Also he uses Java 8 in them, so I flushed those examples out in ~~good~~ old Java 6. |
| 43 | +Since it was a presentation, Jake only put up the most important code snippets in [his slides](https://speakerdeck.com/jakewharton/2014-1). Also he uses Java 8 in them, so I flushed those examples out in ~~good~~ old Java 6. (Note: you're most likely to hit the github api quota pretty fast so send in an oauth-token as a parameter if you want to keep running these examples often). |
44 | 44 |
|
45 | | -## Work in Progress: |
| 45 | +### Orchestrating Observables. Make parallel network calls, then combine the result into a single data point (flatmap + zip) |
46 | 46 |
|
47 | | -### First retrieve from cached data, then make a network call if you can't find your data (concat) (wip) |
| 47 | +The below ascii diagram expresses the intention of our next example with panache. f1,f2,3,f4,f5 are essentially network calls that when made, give back a result that' needed for a future calculation. |
48 | 48 |
|
49 | | -[Courtesy: gist](https://gist.github.com/adelnizamutdinov/7483969) |
50 | 49 |
|
51 | | -### Make two parallel network calls, then combine the result into a single data point (zip) (wip) |
| 50 | + (flatmap) |
| 51 | + f1 ___________________ f3 _______ |
| 52 | + (flatmap) | (zip) |
| 53 | + f2 ___________________ f4 _______| ___________ final output |
| 54 | + \ | |
| 55 | + \____________ f5 _______| |
52 | 56 |
|
53 | | -[Courtesy: gist](https://gist.github.com/adelnizamutdinov/7483969) |
54 | | -http://www.programmableweb.com/category/all/apis?order=field_popularity |
| 57 | +The code for this example has already been written by one Mr.skehlet in the interwebs. Head over to [the gist](https://gist.github.com/skehlet/9418379) for the code. It's written in pure Java (6) so it's pretty comprehensible if you've understood the previous examples. I'll flush it out here again when time permits, and I find a lack of other compelling examples. |
55 | 58 |
|
56 | | -### Orchestrating Observables (flatmap + zip) (wip) |
| 59 | +### Double binding with TextViews () |
57 | 60 |
|
58 | | -If actions A and B depend on action X running; and action C depends on action Y running. What if you want to combine the result of all those calls and have a single output? |
59 | 61 |
|
60 | | - ____________ A ________________ |
61 | | - (flatmap) / | (zip all 3) |
62 | | - X ------------/_____________ B ________________| ----------- > Ouput |
63 | | - | |
64 | | - Y ------------------------- C ________________| |
65 | 62 |
|
66 | | -### Pagination (zip) (wip) |
67 | 63 |
|
68 | | -a. Simple pagination |
69 | | -b. Optimized pagination |
| 64 | +## Work in Progress: |
| 65 | + |
| 66 | +### First retrieve from cached data, if no cache found make a network call if you can't find your data (concat) (wip) |
| 67 | +[Courtesy: gist](https://gist.github.com/adelnizamutdinov/7483969) |
70 | 68 |
|
| 69 | +### Pagination (wip) |
71 | 70 |
|
| 71 | +a. Simple pagination |
| 72 | +b. Optimized pagination |
72 | 73 |
|
73 | | -### Replacing your event Bus (wip) |
| 74 | +### Event Bus with RxJAva (wip) |
74 | 75 |
|
75 | 76 | http://stackoverflow.com/questions/19266834/rxjava-and-random-sporadic-events-on-android |