SlideShare a Scribd company logo
ReactiveX for Android
Ali Muzaffar
http://alimuzaffar.com
What is RxJava
 RxJava is a reactive extension for the java virtual machine.
 What is reactive programming.
“In computing, reactive programming is a programming
paradigm oriented around data flows and the
propagation of change. This means that it should be
possible to express static or dynamic data flows with
ease in the programming languages used, and that the
underlying execution model will automatically
propagate changes through the data flow.” – Wikipeda
 Reactive programming is programming with asynchronous data streams.
 Functional Reactive Programming combines reactive programming and
functional programming. We will be using functional reactive programming.
RxJava Basic Building Blocks
 Observables
 Contains all the heavy processing.
 Emit items (Objects, Strings, Integers etc.).
 Does not start emitting till someone subscribes.
 Subscribers
 Consumes data.
 Manipulation operators (this is the “functional programming” part)
 Transforms
 Filters
 Etc.
 Similar to the observer pattern. Except, doesn’t start emitting till there is a
subscriber.
Observers
 Create a Observer
 Optional
 Which thread to compute on.
 Which thread to return result on.
Subscribers
 Create a subscriber
Simplify Code!
 Reduce boilerplate code.
 Chain method calls.
 Use lambdas.
Data Manipulation Operators
 Used to transform or filter data.
 Keep as much processing on the
background thread as possible.
 Goal is to clean up data and simplify
data processing and code.
Transform Example
 Map() is one of dozens of operators that let you transform data.
 Map does not have to return the same type as the one passed in.
Transform some more…
 What if our observer returns lists of data?
 Just an example, this could be an API call returning a JSON array.
First Approach
 We could just look through the data in our subscriber.
Problem: There is a loop and processing is on the main thread.
Second Approach
 Problem: Loop is gone, but we are still doing a lot of processing on the main
thread and the nested code is ugly.
Third approach using flatMap
 FlatMap takes in data and returns an other Observable. The end subscriber
does not know anything about the data transformation going on.
 Minimal processing on the main thread, simpler code.
RxAndroid
An extension for RxJava built just for Android
RxAndroid
 RxAndroid provides bindings for Android.
 ViewObservable & WidgetObservable provides bindings for views, clicks,
scroll, input etc.
 AndroidObservable provides facilities for working within the Android lifecycle.
Bind to activity, fragment or even broadcasts.
 AndroidSchedulers provides ready for use with the Android multi-threading
system.
ViewObservable & WidgetObservable
 WidgetObservable.text() to capture text change.
 ViewObservable.clicks() to capture clicks.
 Look at Widget and View Example in demo app
AndroidObservable
 Provides bindActivity and bindFragment which automatically use the main
thread for observing.
 Provides fromBroacast which allows you to create an Observable that works
like a BroadcastReceiver.
 They also stop emitting automatically when the activity and fragment are
finishing.
AndroidObservable
 Cache & replay calls continue execution even after we unsubscribe.
 When you subscribe with a new subscriber, they receive all the emitted data.
 Hello World Lifecycle Example in demo app
RxJava has its own issues
 Backpressure
 Android – Memory leaks by Observables because they retain references to
Context.
 Continuing subscription during orientations changes can be difficult.
 Error Handling.
 NotRxAndroid by Jake Wharton
 A from-scratch, re-implementation of RxAndroid with a consistent, opinionated
API. This library will not have releases and is subject to deletion at any time.
Links
 Demo app source https://github.com/alphamu/RxAndroidDemo
 RxJava https://github.com/ReactiveX/RxJava
 RxAndroid https://github.com/ReactiveX/RxAndroid
 Gradle Retro Lambda https://github.com/evant/gradle-retrolambda
 Allows you to use Java 8 features with Android
 Retrofit https://github.com/square/retrofit
 Type-safe REST client for Android and Java by Square, Inc.

More Related Content

PDF
The Mayans Lost Guide to RxJava on Android
PDF
RxJava@Android
PDF
RxJava for Android - GDG DevFest Ukraine 2015
PDF
RxJava on Android
PDF
Intro to RxJava/RxAndroid - GDG Munich Android
PDF
Reactive Android: RxJava and beyond
PDF
rx-java-presentation
PDF
GKAC 2015 Apr. - RxAndroid
The Mayans Lost Guide to RxJava on Android
RxJava@Android
RxJava for Android - GDG DevFest Ukraine 2015
RxJava on Android
Intro to RxJava/RxAndroid - GDG Munich Android
Reactive Android: RxJava and beyond
rx-java-presentation
GKAC 2015 Apr. - RxAndroid

What's hot (20)

PPTX
Reactive programming with RxAndroid
PDF
Practical RxJava for Android
PDF
Practical RxJava for Android
PDF
Introduction to Retrofit and RxJava
PDF
Streams, Streams Everywhere! An Introduction to Rx
PPTX
Reactive Java (33rd Degree)
PDF
Reactive programming with RxJava
PDF
Building Scalable Stateless Applications with RxJava
PPTX
RxJava Applied
PPTX
Reactive Java (GeeCON 2014)
PPTX
Introduction to Reactive Java
PDF
RxJava in practice
PPTX
The Road To Reactive with RxJava JEEConf 2016
PDF
Rxjava 介紹與 Android 中的 RxJava
PDF
Reactive Thinking in Java
PDF
RxJava applied [JavaDay Kyiv 2016]
PPTX
Rx java in action
PDF
My Gentle Introduction to RxJS
PDF
Reactive programming using rx java & akka actors - pdx-scala - june 2014
PDF
Reactive programming on Android
Reactive programming with RxAndroid
Practical RxJava for Android
Practical RxJava for Android
Introduction to Retrofit and RxJava
Streams, Streams Everywhere! An Introduction to Rx
Reactive Java (33rd Degree)
Reactive programming with RxJava
Building Scalable Stateless Applications with RxJava
RxJava Applied
Reactive Java (GeeCON 2014)
Introduction to Reactive Java
RxJava in practice
The Road To Reactive with RxJava JEEConf 2016
Rxjava 介紹與 Android 中的 RxJava
Reactive Thinking in Java
RxJava applied [JavaDay Kyiv 2016]
Rx java in action
My Gentle Introduction to RxJS
Reactive programming using rx java & akka actors - pdx-scala - june 2014
Reactive programming on Android
Ad

Viewers also liked (15)

PDF
RxJava - introduction & design
PPTX
Reactive Programming in Java 8 with Rx-Java
PDF
Reactive programming on Android
PPTX
MVVM and RxJava – the perfect mix
PPTX
Introduction to rx java for android
PPTX
Intro to Functional Programming with RxJava
PDF
Dependency injection on Android
PDF
Java 8 Stream API and RxJava Comparison
PDF
Idiomatic Kotlin for Android
PDF
ReactiveX
PPTX
2 презентация rx java+android
PDF
RxJava for Android - GDG and DataArt
PDF
Dagger & rxjava & retrofit
PDF
RxJava + Retrofit
PDF
Rx java x retrofit
RxJava - introduction & design
Reactive Programming in Java 8 with Rx-Java
Reactive programming on Android
MVVM and RxJava – the perfect mix
Introduction to rx java for android
Intro to Functional Programming with RxJava
Dependency injection on Android
Java 8 Stream API and RxJava Comparison
Idiomatic Kotlin for Android
ReactiveX
2 презентация rx java+android
RxJava for Android - GDG and DataArt
Dagger & rxjava & retrofit
RxJava + Retrofit
Rx java x retrofit
Ad

Similar to Reactive Programming on Android - RxAndroid - RxJava (20)

PDF
RxJava pour Android : présentation lors du GDG Android Montréal
PDF
RxJava@DAUG
PPTX
Introduction to RxJava on Android
PDF
Reactive Functional Programming with Java 8 on Android N
PDF
Streamlining with rx
PDF
Sperasoft Talks: RxJava Functional Reactive Programming on Android
PPT
Presentation - We live in a reactive world - TechForumIberia2016
PPTX
Reactive programming with rx java
PPTX
Rxandroid
PPTX
RxAndroid
PDF
Saving lives with rx java
PDF
Reactive java - Reactive Programming + RxJava
PDF
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
PDF
Reactive Programming for a demanding world: building event-driven and respons...
PPTX
RxJava2 Slides
PPTX
Android asynchronous programming
PPTX
RxJava 2 Reactive extensions for the JVM
PPTX
Intro to Reactive Thinking and RxJava 2
PDF
Iniciación rx java
PPTX
Reactive Programming on Android
RxJava pour Android : présentation lors du GDG Android Montréal
RxJava@DAUG
Introduction to RxJava on Android
Reactive Functional Programming with Java 8 on Android N
Streamlining with rx
Sperasoft Talks: RxJava Functional Reactive Programming on Android
Presentation - We live in a reactive world - TechForumIberia2016
Reactive programming with rx java
Rxandroid
RxAndroid
Saving lives with rx java
Reactive java - Reactive Programming + RxJava
Reactive Programming in Java by Mario Fusco - Codemotion Rome 2015
Reactive Programming for a demanding world: building event-driven and respons...
RxJava2 Slides
Android asynchronous programming
RxJava 2 Reactive extensions for the JVM
Intro to Reactive Thinking and RxJava 2
Iniciación rx java
Reactive Programming on Android

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Advanced IT Governance
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Electronic commerce courselecture one. Pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Empathic Computing: Creating Shared Understanding
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
KodekX | Application Modernization Development
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Cloud computing and distributed systems.
PDF
Sensors and Actuators in IoT Systems using pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Advanced IT Governance
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Big Data Technologies - Introduction.pptx
Chapter 3 Spatial Domain Image Processing.pdf
NewMind AI Weekly Chronicles - August'25 Week I
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Electronic commerce courselecture one. Pdf
Modernizing your data center with Dell and AMD
Empathic Computing: Creating Shared Understanding
“AI and Expert System Decision Support & Business Intelligence Systems”
Per capita expenditure prediction using model stacking based on satellite ima...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Reach Out and Touch Someone: Haptics and Empathic Computing
KodekX | Application Modernization Development
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Cloud computing and distributed systems.
Sensors and Actuators in IoT Systems using pdf
The AUB Centre for AI in Media Proposal.docx

Reactive Programming on Android - RxAndroid - RxJava

  • 1. ReactiveX for Android Ali Muzaffar http://alimuzaffar.com
  • 2. What is RxJava  RxJava is a reactive extension for the java virtual machine.  What is reactive programming. “In computing, reactive programming is a programming paradigm oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming languages used, and that the underlying execution model will automatically propagate changes through the data flow.” – Wikipeda  Reactive programming is programming with asynchronous data streams.  Functional Reactive Programming combines reactive programming and functional programming. We will be using functional reactive programming.
  • 3. RxJava Basic Building Blocks  Observables  Contains all the heavy processing.  Emit items (Objects, Strings, Integers etc.).  Does not start emitting till someone subscribes.  Subscribers  Consumes data.  Manipulation operators (this is the “functional programming” part)  Transforms  Filters  Etc.  Similar to the observer pattern. Except, doesn’t start emitting till there is a subscriber.
  • 4. Observers  Create a Observer  Optional  Which thread to compute on.  Which thread to return result on.
  • 6. Simplify Code!  Reduce boilerplate code.  Chain method calls.  Use lambdas.
  • 7. Data Manipulation Operators  Used to transform or filter data.  Keep as much processing on the background thread as possible.  Goal is to clean up data and simplify data processing and code.
  • 8. Transform Example  Map() is one of dozens of operators that let you transform data.  Map does not have to return the same type as the one passed in.
  • 9. Transform some more…  What if our observer returns lists of data?  Just an example, this could be an API call returning a JSON array.
  • 10. First Approach  We could just look through the data in our subscriber. Problem: There is a loop and processing is on the main thread.
  • 11. Second Approach  Problem: Loop is gone, but we are still doing a lot of processing on the main thread and the nested code is ugly.
  • 12. Third approach using flatMap  FlatMap takes in data and returns an other Observable. The end subscriber does not know anything about the data transformation going on.  Minimal processing on the main thread, simpler code.
  • 13. RxAndroid An extension for RxJava built just for Android
  • 14. RxAndroid  RxAndroid provides bindings for Android.  ViewObservable & WidgetObservable provides bindings for views, clicks, scroll, input etc.  AndroidObservable provides facilities for working within the Android lifecycle. Bind to activity, fragment or even broadcasts.  AndroidSchedulers provides ready for use with the Android multi-threading system.
  • 15. ViewObservable & WidgetObservable  WidgetObservable.text() to capture text change.  ViewObservable.clicks() to capture clicks.  Look at Widget and View Example in demo app
  • 16. AndroidObservable  Provides bindActivity and bindFragment which automatically use the main thread for observing.  Provides fromBroacast which allows you to create an Observable that works like a BroadcastReceiver.  They also stop emitting automatically when the activity and fragment are finishing.
  • 17. AndroidObservable  Cache & replay calls continue execution even after we unsubscribe.  When you subscribe with a new subscriber, they receive all the emitted data.  Hello World Lifecycle Example in demo app
  • 18. RxJava has its own issues  Backpressure  Android – Memory leaks by Observables because they retain references to Context.  Continuing subscription during orientations changes can be difficult.  Error Handling.  NotRxAndroid by Jake Wharton  A from-scratch, re-implementation of RxAndroid with a consistent, opinionated API. This library will not have releases and is subject to deletion at any time.
  • 19. Links  Demo app source https://github.com/alphamu/RxAndroidDemo  RxJava https://github.com/ReactiveX/RxJava  RxAndroid https://github.com/ReactiveX/RxAndroid  Gradle Retro Lambda https://github.com/evant/gradle-retrolambda  Allows you to use Java 8 features with Android  Retrofit https://github.com/square/retrofit  Type-safe REST client for Android and Java by Square, Inc.