SlideShare a Scribd company logo
Reactive Programming dengan
RXJava dan Kotlin Flow
Sidiq Permana
Co founder nbs.dev & Google Developer Expert
Apa yang akan kita pelajari?
● Reactive vs Imperative Programming
● Konsep Utama Reactive Programming
● Asynchronous dan Stream
● Observable - Operator - Observer
● Use Case Umum di Android
● Perbandingan Library
● Konklusi
Reactive vs Imperative
Programming
Dengan pendekatan imperatif, pengembang menulis kode yang
menentukan langkah-langkah yang harus diambil komputer untuk
mencapai tujuan. Ini kadang-kadang disebut sebagai pemrograman
algoritmik. Sebaliknya, pendekatan fungsional melibatkan penyusunan
masalah sebagai seperangkat aliran fungsi yang akan dijalankan untuk
memproses data.
Reactive vs Imperative
Programming
Reactive Programming
● Reactive Programming adalah istilah pemrograman umum yang berfokus
pada reaksi terhadap perubahan, seperti nilai data atau peristiwa (event).
● Dengan kata lain, pemrograman dengan aliran data asinkron yang
mengirimkan data ke konsumen (consumer) saat tersedia.
● Spreadsheet adalah contoh bagus dari pemrograman reaktif: sel yang
bergantung pada sel lain secara otomatis "bereaksi" saat sel lain itu
berubah.Resources.
Reactive Programming
Asynchronous dan Stream
Data
● Asynchronous adalah proses di mana Anda dapat menjalankan perintah
secara bersamaan.
● Stream Data adalah urutan data yang diurutkan dalam waktu
Observable - Operator -
Observer
Contoh
Urutan Pemrosesan Dengan
Chaining Operator
Urutan Pemrosesan Dengan
Chaining Operator
Hot dan Cold Stream
Cold Hot
Data
transmission
time
Lazy stream - new emitting process will start
when someone requests (subscribe)
Eager stream - the emitting process is always
broadcasted even no one asks
Number of
observer
Unicast - only has one observer Multicast - can have multiple observers
Location of data
created
Data is generated outside the stream Data is created in the stream
Simple analogy CDs that need to be played (request) first by
someone before listened
A radio that continues to broadcast even though no
one is listening. Many people can also listening
Contoh Hot Stream
Contoh Cold Stream
Contoh Cold Stream
Faedah menggunakan
Reactive Programming
Faedah Utama
● Aplikasi lebih reaktif terhadap event! Seperti memproses event
pengguna seperti ketuk dua kali, pencarian, tombol validasi, dan UX
kompleks lainnya.
● Mengakses data dari sumber data secara asinkron tanpa callback hell.
● Memproses data yang diperoleh dari API, seperti mengurutkan dan
memfilter data.
● Multi threading yang lebih baik.
Aplikasi Berasa Lebih Reaktif
Lebih Fokus Pada Hasil Akhir
Ketimbang Prosesnya
fun register(newUser: NewUser) {
val username: String = newUser.getUsername()
val password: String = newUser.getPassword()
api.register(newUser, object : Callback<Boolean>() {
fun onResponse(success: Boolean) {
if (success) {
api.login(username, password), object :
Callback<Token>() {
fun onResponse(token: Token) {
api.getUser(token, object :
Callback<UserDetails>() {
fun onResponse(userDetails:
UserDetails) {
// continue with app flow
}
})
}
})
}
}
})
}
fun register(newUser: NewUser) {
val username: String = newUser.getUsername()
val password: String = newUser.getPassword()
api.register(newUser)
.filter { success -> success }
.flatMap { success ->
api.login(username, password) }
.flatMap { token -> api.getUser(token) }
.subscribe { userDetails ->
// continue with app flow }
}
Lebih Fokus Pada Hasil Akhir
Ketimbang Prosesnya
Lebih Fokus Pada Hasil Akhir
Ketimbang Prosesnya
Multithreading yang lebih
cakep!
observeSomething()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { name -> println(name)}
CoroutineScope(Dispatchers.Main).launch {
observeSomething()
.flowOn(Dispatchers.IO)
.collect { name -> println(name) }
}
Reactive Library
Membuat Data Stream
Observable.just("1", "2", "3", "4", "5", "6")
.map { string -> string.toInt() }
.filter { number -> number%2 == 1 }
.subscribe { result -> println("$result is odd number") }
Dengan Observable RXJava
CoroutineScope.launch {
flowOf("1", "2", "3", "4", "5", "6")
.map { string -> string.toInt() }
.filter { number -> number % 2 == 1 }
.collect { result -> println("$result is odd number") }
}
Dengan Flow
Tipe Stream
RxJava
● Observable: to emits unlimited data streams.
● Flowable: to emits unlimited data streams with back pressure strategy.
● Single: to emits one data only.
● Maybe: same as Single, but it can be no data.
● Completable: stream which can only complete without emitting values.
Flow
● Flow: There is no separate implementations without and with back pressure
support in Kotlin Flow.
Backpressure
Backpressure is a situation where the Publisher provides excessive data so that
the Subscriber cannot accommodate it. There are several back pressure
strategies to choose :
● BUFFER: Processes received data and waits for other data in the queue.
● DROP: Processes received data and ignores other data.
● LATEST: Processes received data, but stores the last element in cache.
● MISSING: Processes without using Backpressure Strategy, so you have to
make your own custom.
● ERROR: Notifies MissingBackpressureException error if process is not
robust.
Multithreading
observeSomething()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { name -> println(name)}
CoroutineScope(Dispatchers.Main).launch {
observeSomething()
.flowOn(Dispatchers.IO)
.collect { name -> println(name) }
}
Common Schedulers and
Dispatcher
● io: to performs processes on the I/O Thread such as calling API and writing
databases.
● computation: to performs processes with high CPU requirements such as
converting bitmaps or compressing.
● main: to receives the result of the run on the Android Main/UI Thread.
● single: useful if you have multiple processes originating from different places
and want to merge them.
● custom: creates own custom scheduler with the help of Executor.
Perbandingan
RxJava Flow
Observable Types Observable, Flowable, Single, Maybe, Completable Flow (Only one)
Create Observable create, just, dll. flowOf
Get data subscribe collect
Context preservation subscribeOn, observeOn flowOn
Thread type Schedulers Dispatchers
Backpressure Manual By default handled by Flow
Request with Retrofit Need RxJava Adapter Can be used directly
Database with Room Need library Room RxJava Need library Room ktx
Lifecycle Management Using Disposable Using Scope
Kesimpulan
● RxJava dan Coroutine Flow memiliki kelebihan dan kekurangan dalam
implementasi terlebih dalam learning curve ketika adopsinya.
● Penggunaan tools tidak terlepas dari kebutuhan dan pemahaman yang
dimiliki kadang juga karena selera!
● Mempelajari dan memahami keduanya akan memperbanyak pilihan opsi
untuk penyelesaian masalah dengan tools yang tepat!
● Give it try and Enjoy!
Contact:
sidiq@nbs.co.id
THANKS!
Follow us: @dicoding

More Related Content

Similar to Dicoding Developer Coaching #32: Android | Reactive Programming dengan RxJava & Flow (20)

PDF
Building Scalable Stateless Applications with RxJava
Rick Warren
 
PPTX
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
PPTX
Intro to Functional Programming with RxJava
Mike Nakhimovich
 
PDF
Reactive Programming with NetBeans and Java 8
Stefan Reuter
 
PDF
Understanding Reactive Programming
Andres Almiray
 
PPTX
Reactive programming with RxAndroid
Savvycom Savvycom
 
PPTX
Rxandroid
Thinh Thanh
 
PPTX
RxAndroid
Thinh Thanh
 
PDF
Springone2gx 2014 Reactive Streams and Reactor
Stéphane Maldini
 
PDF
Reactive Fault Tolerant Programming with Hystrix and RxJava
Matt Stine
 
PDF
Reactive Functional Programming with Java 8 on Android N
Shipeng Xu
 
PDF
Introdução à Spring Web Flux
Wellington Gustavo Macedo
 
PPTX
Introduction to Reactive programming
Dwi Randy Herdinanto
 
PDF
Reactive programming on Android
Tomáš Kypta
 
PDF
RxJava in practice
Javier Gamarra
 
PDF
Guide to Spring Reactive Programming using WebFlux
Inexture Solutions
 
PPTX
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PDF
RxJava - introduction & design
allegro.tech
 
PPTX
RxJava2 Slides
YarikS
 
PPTX
From Web to Flux @DevoxxBE 2023.pptx
Victor Rentea
 
Building Scalable Stateless Applications with RxJava
Rick Warren
 
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
Intro to Functional Programming with RxJava
Mike Nakhimovich
 
Reactive Programming with NetBeans and Java 8
Stefan Reuter
 
Understanding Reactive Programming
Andres Almiray
 
Reactive programming with RxAndroid
Savvycom Savvycom
 
Rxandroid
Thinh Thanh
 
RxAndroid
Thinh Thanh
 
Springone2gx 2014 Reactive Streams and Reactor
Stéphane Maldini
 
Reactive Fault Tolerant Programming with Hystrix and RxJava
Matt Stine
 
Reactive Functional Programming with Java 8 on Android N
Shipeng Xu
 
Introdução à Spring Web Flux
Wellington Gustavo Macedo
 
Introduction to Reactive programming
Dwi Randy Herdinanto
 
Reactive programming on Android
Tomáš Kypta
 
RxJava in practice
Javier Gamarra
 
Guide to Spring Reactive Programming using WebFlux
Inexture Solutions
 
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
RxJava - introduction & design
allegro.tech
 
RxJava2 Slides
YarikS
 
From Web to Flux @DevoxxBE 2023.pptx
Victor Rentea
 

More from DicodingEvent (20)

PDF
Developer Coaching #114.pdf
DicodingEvent
 
PDF
Ask Us Anything about Studi Independen Bersertifikat Kampus Merdeka X Dicodin...
DicodingEvent
 
PPTX
tantangan menjadi developer di abad 21
DicodingEvent
 
PDF
Mengenalkan augmented reality (ar) pada snapchat
DicodingEvent
 
PDF
Membangun Aplikasi Serverless di Platfrom AWS
DicodingEvent
 
PDF
IDCamp X Madrasah: Pengenalan Computational Thinking
DicodingEvent
 
PDF
Membuat Produk Digital Terbaik ala Startup Unicorn
DicodingEvent
 
PDF
TechTalk 2021: Peran IT Security dalam Penerapan DevOps
DicodingEvent
 
PDF
TechTalk 2021: Peningkatan Performa Software Delivery dengan CI/CD
DicodingEvent
 
PDF
Membuat Solusi Bermanfaat dengan Programming - Nur Rohman
DicodingEvent
 
PDF
Potensi karier menjadi ios developer di masa depan
DicodingEvent
 
PDF
Id camp x dicoding live : persiapan jadi software engineer hebat 101
DicodingEvent
 
PDF
Tips sukses berkarir sebagai developer dan programmer 2021
DicodingEvent
 
PPTX
Teknologi Baru Android di Google I/O 2021 - Andrew Kurniadi
DicodingEvent
 
PDF
Dicoding Developer Coaching #38: Android | 5 Library Android yang Patut Kamu ...
DicodingEvent
 
PDF
Dicoding Developer Coaching #37: Android | Kesalahan yang Sering Terjadi pada...
DicodingEvent
 
PDF
Pengantar Cloud Computing dengan AWS - Petra Novandi Barus
DicodingEvent
 
PDF
Dicoding Developer Coaching #36: Android | Pentingnya Performa pada Aplikasi ...
DicodingEvent
 
PDF
Dicoding Developer Coaching #34: Android | Modular Android App dengan Dynamic...
DicodingEvent
 
PDF
Dicoding Developer Coaching #35: Android | Setup Continuous Integration di An...
DicodingEvent
 
Developer Coaching #114.pdf
DicodingEvent
 
Ask Us Anything about Studi Independen Bersertifikat Kampus Merdeka X Dicodin...
DicodingEvent
 
tantangan menjadi developer di abad 21
DicodingEvent
 
Mengenalkan augmented reality (ar) pada snapchat
DicodingEvent
 
Membangun Aplikasi Serverless di Platfrom AWS
DicodingEvent
 
IDCamp X Madrasah: Pengenalan Computational Thinking
DicodingEvent
 
Membuat Produk Digital Terbaik ala Startup Unicorn
DicodingEvent
 
TechTalk 2021: Peran IT Security dalam Penerapan DevOps
DicodingEvent
 
TechTalk 2021: Peningkatan Performa Software Delivery dengan CI/CD
DicodingEvent
 
Membuat Solusi Bermanfaat dengan Programming - Nur Rohman
DicodingEvent
 
Potensi karier menjadi ios developer di masa depan
DicodingEvent
 
Id camp x dicoding live : persiapan jadi software engineer hebat 101
DicodingEvent
 
Tips sukses berkarir sebagai developer dan programmer 2021
DicodingEvent
 
Teknologi Baru Android di Google I/O 2021 - Andrew Kurniadi
DicodingEvent
 
Dicoding Developer Coaching #38: Android | 5 Library Android yang Patut Kamu ...
DicodingEvent
 
Dicoding Developer Coaching #37: Android | Kesalahan yang Sering Terjadi pada...
DicodingEvent
 
Pengantar Cloud Computing dengan AWS - Petra Novandi Barus
DicodingEvent
 
Dicoding Developer Coaching #36: Android | Pentingnya Performa pada Aplikasi ...
DicodingEvent
 
Dicoding Developer Coaching #34: Android | Modular Android App dengan Dynamic...
DicodingEvent
 
Dicoding Developer Coaching #35: Android | Setup Continuous Integration di An...
DicodingEvent
 
Ad

Recently uploaded (19)

PPTX
STURGEON BAY WI AG PPT JULY 6 2025.pptx
FamilyWorshipCenterD
 
PDF
The Origin - A Simple Presentation on any project
RishabhDwivedi43
 
PPTX
Inspired by VeinSense: Supercharge Your Hackathon with Agentic AI
ShubhamSharma2528
 
DOCX
How Digital Marketplaces are Empowering Emerging MedTech Brands
Ram Gopal Varma
 
PDF
Buy Verified Coinbase Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
PDF
From Draft to DSN - How to Get your Paper In [DSN 2025 Doctoral Forum Keynote]
vschiavoni
 
PPTX
some leadership theories MBA management.pptx
rkseo19
 
PDF
The Impact of Game Live Streaming on In-Game Purchases of Chinese Young Game ...
Shibaura Institute of Technology
 
PPTX
BARRIERS TO EFFECTIVE COMMUNICATION.pptx
shraddham25
 
PPTX
Great-Books. Powerpoint presentation. files
tamayocrisgie
 
PPTX
Presentationexpressions You are student leader and have just come from a stud...
BENSTARBEATZ
 
PDF
Model Project Report_36DR_G&P.pdf for investors understanding
MeetAgrawal23
 
PDF
Committee-Skills-Handbook---MUNprep.org.pdf
SatvikAgarwal9
 
PPTX
presentation on legal and regulatory action
raoharsh4122001
 
PPTX
Pastor Bob Stewart Acts 21 07 09 2025.pptx
FamilyWorshipCenterD
 
PDF
Buy Verified Payoneer Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
PPTX
AI presentation for everyone in every fields
dodinhkhai1
 
PDF
The Family Secret (essence of loveliness)
Favour Biodun
 
PDF
Leveraging the Power of Jira Dashboard.pdf
siddharthshukla742740
 
STURGEON BAY WI AG PPT JULY 6 2025.pptx
FamilyWorshipCenterD
 
The Origin - A Simple Presentation on any project
RishabhDwivedi43
 
Inspired by VeinSense: Supercharge Your Hackathon with Agentic AI
ShubhamSharma2528
 
How Digital Marketplaces are Empowering Emerging MedTech Brands
Ram Gopal Varma
 
Buy Verified Coinbase Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
From Draft to DSN - How to Get your Paper In [DSN 2025 Doctoral Forum Keynote]
vschiavoni
 
some leadership theories MBA management.pptx
rkseo19
 
The Impact of Game Live Streaming on In-Game Purchases of Chinese Young Game ...
Shibaura Institute of Technology
 
BARRIERS TO EFFECTIVE COMMUNICATION.pptx
shraddham25
 
Great-Books. Powerpoint presentation. files
tamayocrisgie
 
Presentationexpressions You are student leader and have just come from a stud...
BENSTARBEATZ
 
Model Project Report_36DR_G&P.pdf for investors understanding
MeetAgrawal23
 
Committee-Skills-Handbook---MUNprep.org.pdf
SatvikAgarwal9
 
presentation on legal and regulatory action
raoharsh4122001
 
Pastor Bob Stewart Acts 21 07 09 2025.pptx
FamilyWorshipCenterD
 
Buy Verified Payoneer Accounts — The Ultimate Guide for 2025 (Rank #1 on Goog...
Buy Verified Cash App Accounts
 
AI presentation for everyone in every fields
dodinhkhai1
 
The Family Secret (essence of loveliness)
Favour Biodun
 
Leveraging the Power of Jira Dashboard.pdf
siddharthshukla742740
 
Ad

Dicoding Developer Coaching #32: Android | Reactive Programming dengan RxJava & Flow

  • 1. Reactive Programming dengan RXJava dan Kotlin Flow Sidiq Permana Co founder nbs.dev & Google Developer Expert
  • 2. Apa yang akan kita pelajari? ● Reactive vs Imperative Programming ● Konsep Utama Reactive Programming ● Asynchronous dan Stream ● Observable - Operator - Observer ● Use Case Umum di Android ● Perbandingan Library ● Konklusi
  • 3. Reactive vs Imperative Programming Dengan pendekatan imperatif, pengembang menulis kode yang menentukan langkah-langkah yang harus diambil komputer untuk mencapai tujuan. Ini kadang-kadang disebut sebagai pemrograman algoritmik. Sebaliknya, pendekatan fungsional melibatkan penyusunan masalah sebagai seperangkat aliran fungsi yang akan dijalankan untuk memproses data.
  • 5. Reactive Programming ● Reactive Programming adalah istilah pemrograman umum yang berfokus pada reaksi terhadap perubahan, seperti nilai data atau peristiwa (event). ● Dengan kata lain, pemrograman dengan aliran data asinkron yang mengirimkan data ke konsumen (consumer) saat tersedia. ● Spreadsheet adalah contoh bagus dari pemrograman reaktif: sel yang bergantung pada sel lain secara otomatis "bereaksi" saat sel lain itu berubah.Resources.
  • 7. Asynchronous dan Stream Data ● Asynchronous adalah proses di mana Anda dapat menjalankan perintah secara bersamaan. ● Stream Data adalah urutan data yang diurutkan dalam waktu
  • 12. Hot dan Cold Stream Cold Hot Data transmission time Lazy stream - new emitting process will start when someone requests (subscribe) Eager stream - the emitting process is always broadcasted even no one asks Number of observer Unicast - only has one observer Multicast - can have multiple observers Location of data created Data is generated outside the stream Data is created in the stream Simple analogy CDs that need to be played (request) first by someone before listened A radio that continues to broadcast even though no one is listening. Many people can also listening
  • 17. Faedah Utama ● Aplikasi lebih reaktif terhadap event! Seperti memproses event pengguna seperti ketuk dua kali, pencarian, tombol validasi, dan UX kompleks lainnya. ● Mengakses data dari sumber data secara asinkron tanpa callback hell. ● Memproses data yang diperoleh dari API, seperti mengurutkan dan memfilter data. ● Multi threading yang lebih baik.
  • 19. Lebih Fokus Pada Hasil Akhir Ketimbang Prosesnya fun register(newUser: NewUser) { val username: String = newUser.getUsername() val password: String = newUser.getPassword() api.register(newUser, object : Callback<Boolean>() { fun onResponse(success: Boolean) { if (success) { api.login(username, password), object : Callback<Token>() { fun onResponse(token: Token) { api.getUser(token, object : Callback<UserDetails>() { fun onResponse(userDetails: UserDetails) { // continue with app flow } }) } }) } } }) } fun register(newUser: NewUser) { val username: String = newUser.getUsername() val password: String = newUser.getPassword() api.register(newUser) .filter { success -> success } .flatMap { success -> api.login(username, password) } .flatMap { token -> api.getUser(token) } .subscribe { userDetails -> // continue with app flow } }
  • 20. Lebih Fokus Pada Hasil Akhir Ketimbang Prosesnya
  • 21. Lebih Fokus Pada Hasil Akhir Ketimbang Prosesnya
  • 22. Multithreading yang lebih cakep! observeSomething() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { name -> println(name)} CoroutineScope(Dispatchers.Main).launch { observeSomething() .flowOn(Dispatchers.IO) .collect { name -> println(name) } }
  • 24. Membuat Data Stream Observable.just("1", "2", "3", "4", "5", "6") .map { string -> string.toInt() } .filter { number -> number%2 == 1 } .subscribe { result -> println("$result is odd number") } Dengan Observable RXJava CoroutineScope.launch { flowOf("1", "2", "3", "4", "5", "6") .map { string -> string.toInt() } .filter { number -> number % 2 == 1 } .collect { result -> println("$result is odd number") } } Dengan Flow
  • 25. Tipe Stream RxJava ● Observable: to emits unlimited data streams. ● Flowable: to emits unlimited data streams with back pressure strategy. ● Single: to emits one data only. ● Maybe: same as Single, but it can be no data. ● Completable: stream which can only complete without emitting values. Flow ● Flow: There is no separate implementations without and with back pressure support in Kotlin Flow.
  • 26. Backpressure Backpressure is a situation where the Publisher provides excessive data so that the Subscriber cannot accommodate it. There are several back pressure strategies to choose : ● BUFFER: Processes received data and waits for other data in the queue. ● DROP: Processes received data and ignores other data. ● LATEST: Processes received data, but stores the last element in cache. ● MISSING: Processes without using Backpressure Strategy, so you have to make your own custom. ● ERROR: Notifies MissingBackpressureException error if process is not robust.
  • 27. Multithreading observeSomething() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { name -> println(name)} CoroutineScope(Dispatchers.Main).launch { observeSomething() .flowOn(Dispatchers.IO) .collect { name -> println(name) } }
  • 28. Common Schedulers and Dispatcher ● io: to performs processes on the I/O Thread such as calling API and writing databases. ● computation: to performs processes with high CPU requirements such as converting bitmaps or compressing. ● main: to receives the result of the run on the Android Main/UI Thread. ● single: useful if you have multiple processes originating from different places and want to merge them. ● custom: creates own custom scheduler with the help of Executor.
  • 29. Perbandingan RxJava Flow Observable Types Observable, Flowable, Single, Maybe, Completable Flow (Only one) Create Observable create, just, dll. flowOf Get data subscribe collect Context preservation subscribeOn, observeOn flowOn Thread type Schedulers Dispatchers Backpressure Manual By default handled by Flow Request with Retrofit Need RxJava Adapter Can be used directly Database with Room Need library Room RxJava Need library Room ktx Lifecycle Management Using Disposable Using Scope
  • 30. Kesimpulan ● RxJava dan Coroutine Flow memiliki kelebihan dan kekurangan dalam implementasi terlebih dalam learning curve ketika adopsinya. ● Penggunaan tools tidak terlepas dari kebutuhan dan pemahaman yang dimiliki kadang juga karena selera! ● Mempelajari dan memahami keduanya akan memperbanyak pilihan opsi untuk penyelesaian masalah dengan tools yang tepat! ● Give it try and Enjoy!