Reactivex with Kotlin

A presentation at Lubumbashi Kotlin Night in November 2019 in Lubumbashi, Democratic Republic of the Congo by Jonathan Monga

Slide 1

Slide 1

Reactivex with Kotlin 1

Slide 2

Slide 2

Jonathan Monga Android Developer Maishapay, Wenzeasy, CongoBD JonathanMonga @jonathan_monga 2

Slide 3

Slide 3

A little survey 3

Slide 4

Slide 4

A little survey ● Who are Android Developer ? ● Who are not Android Developer ? ● Who develops with Kotlin ? ● Who develops with Java ? ● Who heard about reactivex ? 4

Slide 5

Slide 5

Agenda 5

Slide 6

Slide 6

Agenda ● Boring definitions ● Why RxKotlin ? ● Deep dive into RxKotlin 6

Slide 7

Slide 7

Boring definitions 7

Slide 8

Slide 8

Boring definitions 1) Reactive programming « Is general term thas is focused on reacting to changes, such as data values or events. » Ben christensen. 8

Slide 9

Slide 9

Boring definitions 2) Reactives extensions ● ● An API to handle events synchronously or asynchronously through a flow of event. « Une bibliothèque permettant de composer des programmes asynchrones et basés sur des événements à l’aide de séquences observables. » 9

Slide 10

Slide 10

Boring definitions 3) What’s RxJava or RxKotlin ● ● The java implementation of Reactive extensions. Une Une bibliothèque très légère qui apporte ou ajoute de fonctions d’extensions pratique à RxJava, vu l’interopérabilité entre Java et Kotlin, on peut utiliser RxJava directement avec Kotlin. 10

Slide 11

Slide 11

Boring definitions 4) Reactives extensions (Once upon a time) 2007 2007 — RX RX by by Erik Erik Meijer Meijer 2012 2012 –– RxJava RxJava 11 by by Ben Ben Christensen Christensen 11

Slide 12

Slide 12

Boring definitions 4) Reactives extensions (Once upon a time) 2007 2007 — RX RX by by Erik Erik Meijer Meijer 2012 2012 –– RxJava RxJava 11 by by Ben Ben Christensen Christensen 12

Slide 13

Slide 13

Boring definitions 4) Reactives extensions (Once upon a time) 2013 2013 — RxJava RxJava 22 by by Dávid Dávid Karnok Karnok 2014 2014 –– RxKotlin RxKotlin 11 && 22 by by Ben Ben Christensen Christensen && Thomas Thomas Nield Nield 13

Slide 14

Slide 14

Why RxKotlin ? 14

Slide 15

Slide 15

What is the Problem ? 15

Slide 16

Slide 16

What is the solution? 16

Slide 17

Slide 17

What is the Problem ? 17

Slide 18

Slide 18

What is the solution? 18

Slide 19

Slide 19

What is the Problem ? 19

Slide 20

Slide 20

What is the solution? 20

Slide 21

Slide 21

In conclusion… (a) Get rid of the callback hell. (b) Standard mechanism for error handling (c) It’s a lot simpler than regular threading (d) Simpler way for async operation (e) The same api for every operation (f) The functionnal way (g) Maintainable & Testable code (h) Because of Kotlin. 21

Slide 22

Slide 22

Et Google? 22

Slide 23

Slide 23

Deep dive into RxKotlin 23

Slide 24

Slide 24

Pattern Observer (a) Observable (b) Observer (c) Operateurs (d) Schedulers 24

Slide 25

Slide 25

(a) Observable Is a function that when invoked return 0 ~ ∞ values between now and the end of times. Paul Taylor | Reactive 2015 One value Many values Synchronous/ Pull Fonction Iterator/ Iterable Asynchronous/ Push Promise/ Future Observable 25

Slide 26

Slide 26

(a) Observable Is a function that when invoked return 0 ~ ∞ values between now and the end of times. Paul Taylor | Reactive 2015 One value Many values Synchronous/ Pull Fonction Iterator/ Iterable Asynchronous/ Push Promise/ Future Observable 26

Slide 27

Slide 27

(a) Observable Is a function that when invoked return 0 ~ ∞ values between now and the end of times. Paul Taylor | Reactive 2015 One value Many values Synchronous/ Pull Fonction Iterator/ Iterable Asynchronous/ Push Promise/ Future Observable 27

Slide 28

Slide 28

(a) Observable Is a function that when invoked return 0 ~ ∞ values between now and the end of times. Paul Taylor | Reactive 2015 One value Many values Synchronous/ Pull Fonction Iterator/ Iterable Asynchronous/ Push Promise/ Future Observable 28

Slide 29

Slide 29

(b) Observer It is an interface with 3 main methods. Iterable (Pull) Observer (Push) Get data <T> next() onNext(<T>) To see the error <T> next() throws Exception onError(Throwable) To complete boolean hasnext() onComplete() 29

Slide 30

Slide 30

(b) Observer It is an interface with 3 main methods. Iterable (Pull) Observer (Push) Get data <T> next() onNext(<T>) To see the error <T> next() throws Exception onError(Throwable) To complete boolean hasnext() onComplete() 30

Slide 31

Slide 31

(b) Observer It is an interface with 3 main methods. Iterable (Pull) Observer (Push) Get data <T> next() onNext(<T>) To see the error <T> next() throws Exception onError(Throwable) To complete boolean hasnext() onComplete() 31

Slide 32

Slide 32

(c) Type of Observable 32

Slide 33

Slide 33

(d) Subjects Intersection of Observable and observer 33

Slide 34

Slide 34

(d) Subjects Intersection of Observable and observer 34

Slide 35

Slide 35

(e) How to emit events 35

Slide 36

Slide 36

Operators a) Marble diagrams 36

Slide 37

Slide 37

Operators (b) Type of operators 37

Slide 38

Slide 38

Operators (c) How to use operators 38

Slide 39

Slide 39

Operators Further exemple 39

Slide 40

Slide 40

Schedulers Utiliser pour definir un context d’execution. ● Dans quel thread mon observer va-t-il s’abonner à l’observable ? Use subscribeOn() method. ● Dans quel thread mon observer va-t-il observer ? Use observeOn() method. 40

Slide 41

Slide 41

Schedulers List of schedulers 41

Slide 42

Slide 42

Schedulers Example of schedulers 42

Slide 43

Slide 43

Leaning path & Resource - Training / RxJava2-Android-Samples Guide by Arke Team Reactive Programming with RxJava

  • kaushikgopal/RxJava-Android-Samples - Introduction to RxKotlin Guide Video - Exploring RxJava 2 for Android Jake Wharton - Intro to RxJava for Android - Intro to RxJava Examples Official Site Pratice and Google
  • ReactiveX Official Site
  • Practice in Project
  • ReactiveX GitHub
  • Google and Thinking
  • RxJava GitHub - RxKotlin GitHub - RxJava Category in my Blog 43

Slide 44

Slide 44

Conclusion ● ● ● ● Le pattern Observable/Observer peut s’appliquer à tout RxJava ou RxKotlin sont des technologies approuvées, déjà utiliser chez NetFlix en production. RxJava ou RxKotlin sont des outils très riches. Demande une certaine maîtrise, c’est à dire la courbe d’apprentissage est un peu plus longue. 44

Slide 45

Slide 45

In conclusion… (a) Get rid of the callback hell. (b) It’s a lot simpler than regular threading (c) The same api for every operation (d) Maintainable & Testable code 45

Slide 46

Slide 46

Fin Jonathan Monga Android Developer JonathanMonga @jonathan_monga 46