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

Introduction to RxJava

Introduction to RxJava

Introductory presentation for Reactive Programming in Android Workshop - GDG Kolkata, November 2017

Rivu Chakraborty

November 11, 2017
Tweet

More Decks by Rivu Chakraborty

Other Decks in Technology

Transcript

  1. Reactive Programming in Android Observables, Observers, Operators, Schedulers Rivu Chakraborty

    Google Certified Android Developer Author of Reactive Programming in Kotlin Android and Kotlin Enthusiast, Kotlin Evangelist https://www.packtpub.com/application-development/reactive-programming-kotlin
  2. Java 8 Streams vs Observables There are significant differences between

    Observable and Stream. Streams are pull-based, Observables are push-based. This may sound too abstract, but it has significant consequences that are very concrete. Stream can only be used once, Observable can be subscribed to many times Streams are lacking time-related operations like Observable#interval(), Observable#timer() and many others; this is mostly because Streams are pull-based Streams are lacking filtering/suppressing operators (takeWhile(), takeUntil(), filter() etc); workaround using Stream#anyMatch() is limited: it is terminal operation, so you can't use it more than once per stream Focus: Pull-Based, Push-Based
  3. Observable and Observer Observable has an underlying Computation that produces

    values that can be consumed by a consumer (Observer). The most important thing here is that the consumer (Observer) doesn’t pull values here; rather Observable pushes the value to the consumer. So we may say, an Observable is a push- based, composable iterator that emits its items through a series of operators to the final Observer, which finally consumes the items. Let us now break things in sequentially to understand it better. • An Observer Subscribes to an Observable • Observable starts emitting items, that it has in it • Observer Reacts to whatever item the Observable emits Source – Reactive Programming in Kotlin Chapter 3 - Observables, Observers and Subjects
  4. Subscribing Observer An Observer is like a listener which listens

    to Observable events. an Observable has three most important events/methods, let us discuss them one by one. • onNext: Observable passes all items one by one to this method • onComplete: When all items have gone through the onNext method, Observable calls onComplete method. • onError: When Observable faces any error, it calls onError method to deal with the error if defined. One thing to note here, the item in Observable that we are talking about can be anything, it is defined as Observable<T>, where T can be any class, even an array/list can be assigned as an Observable. Source – Reactive Programming in Kotlin Chapter 3 - Observables, Observers and Subjects
  5. Operators Operators work like a consumer to the preceding Observable

    / Flowable, listens to their emissions, transforms them and emits them to the downstream consumer. For instance, think of the map operator, the map operator, it listens to the upstream producer, performs some operations on their emissions, then emits those modified items to the downstream.
  6. Operators • map the map operator listens to the upstream

    producer, performs some operations on their emissions, then emits those modified items to the downstream. • filter Think of a situation when you want to receive some emissions from the producer and discarding the rest. The filter Operator is there to Help you • flatMap Transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable Source – Reactive Programming in Kotlin Chapter 5 - Asynchronous Data Operators and Transformations