Slide 1

Slide 1 text

Reactive Android
 just a little introduction! Presented by Akhyar Amarullah
 at Engineering Brownbag Session in Ansvia
 on Friday, July 10th 2015"

Slide 2

Slide 2 text

“In computing, reactive programming is 
 a programming paradigm oriented around 
 data flows and the propagation of change.” !

Slide 3

Slide 3 text

RxJava! •  RxJava is reactive extension implemented on Java Virtual Machin. ! •  Other language implementations also available: RxScala, RxGroovy, RxKotlin, RxJS, etc. 
 All of them are implemented with respect to respective languages’ style & idioms.! •  RxAndroid is RxJava + Android helper classes!

Slide 4

Slide 4 text

Ease Android Development" •  Doing long running tasks in background thread and fire the callbacks on UI thread (actually, any threads)! •  Chaining sequential tasks without callback hell ! •  Using multiple data sources (e.g. remote API, disk cache, memory cache, etc)! •  Batch processing big amount of data into small chunks! •  Threat anything as data streams:! –  Social stream from API! •  Streams are flexible, you can:! –  Transform! –  Filter! –  Throttle! –  Combine it with other streams! •  Observables can act as event bus! •  Supported by Retrofit library out of the box!

Slide 5

Slide 5 text

RxJava Building Blocks! •  Observable! –  Source of data! –  Emits items (Objects, Strings, etc)! –  There are two types:! •  Cold observable! •  Hot observable! •  Observer/Subscriber! –  Consumes data! •  Operators! –  Combining: concat, merge, zip, join, etc! –  Filtering: filter, first, last, etc! –  Transforming: map, flatMap, groupBy, buffer, scan, etc!

Slide 6

Slide 6 text

Observable! •  Source of data! •  Basic building block of reactive programming! •  Composable! •  It doesn’t care of its implementation:! – Synchronous or asynchronous! – Which threads are you using! – Use actors or thread pools! – etc!

Slide 7

Slide 7 text

Observable!

Slide 8

Slide 8 text

Observable vs Iterable!

Slide 9

Slide 9 text

Observable Callbacks! •  Observable   – onNext(T  t): emits data! – onError(Throwable  e): when error occurs and the observable should terminate! – onComplete(): when no more data available and observable terminates without error!

Slide 10

Slide 10 text

Observable Callbacks!

Slide 11

Slide 11 text

Observable Marble Diagram!

Slide 12

Slide 12 text

RxJava on Actions"

Slide 13

Slide 13 text

Setting Up RxJava for Android! •  compile  'io.reactivex:rxandroid:+'   •  Optional but highly recommended: ! apply  plugin:  'com.android.application'   apply  plugin:  'me.tatarka.retrolambda'   buildscript  {          repositories  {                  mavenCentral()          }          dependencies  {                  classpath  'me.tatarka:gradle-­‐retrolambda:+'          }   }   !

Slide 14

Slide 14 text

Creating Observable
 for Long-Running Tasks" •  Use simpler defer() or more verbose create()! •  Do not forget to check for subscriber. It might be already unsubscribed.!

Slide 15

Slide 15 text

Creating Observable
 using Retrofit"

Slide 16

Slide 16 text

Subscribing to Observable"

Slide 17

Slide 17 text

Batch Processing in 10-sized chunks" Divide and conquer!!

Slide 18

Slide 18 text

Multiple Data Source with Fallback" Credit: http://blog.danlew.net/2015/06/22/loading-data-from- multiple-sources-with-rxjava/!

Slide 19

Slide 19 text

Chaining sequential tasks! Callback HELL @!#$!$@#" Much better and readable"

Slide 20

Slide 20 text

Handling view clicks" With throttling!!

Slide 21

Slide 21 text

おわり!