Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reactive Android: just a little introduction

Reactive Android: just a little introduction

Presented at Engineering Brown Bag Session in Ansvia

Akhyar Amarullah

July 10, 2015
Tweet

More Decks by Akhyar Amarullah

Other Decks in Programming

Transcript

  1. Reactive Android
 just a little introduction! Presented by Akhyar Amarullah


    at Engineering Brownbag Session in Ansvia
 on Friday, July 10th 2015"
  2. “In computing, reactive programming is 
 a programming paradigm oriented

    around 
 data flows and the propagation of change.” !
  3. 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!
  4. 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!
  5. 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!
  6. 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!
  7. Observable Callbacks! •  Observable<T>   – 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!
  8. 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:+'          }   }   !
  9. 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.!