SlideShare uma empresa Scribd logo
1 de 27
Intro to RxJava on Android
Chris Arriola
Functional Reactive Programming
Makes dealing with concurrency easy
Makes code a lot more concise and readable
Encourages defensive programming and makes error handling easy
Increases the level of abstraction
Why consider RxJava?
Nested Network Call w/o RxJava
Nested Network Call w/ RxJava
What is RxJava?
Java implementation of .NET’s Reactive Extensions
Specification for Functional Reactive Programming (FRP)
Programming with asynchronous data streams
Not a new concept. Think: click events/handlers, event bus, etc.
Reactive part
Can combine, create, filter, map, or transform any stream
Core RxJava Constructs
● Observable
● Observer
● Operator
Emits items in a sequence
Like an Iterator, it produces all items in a sequence
Each emitted item will be propagated to each Observer
Observable
Observer
Observer (aka the “subscriber”) subscribes to Observable
Observers are notified of a new item through #onNext(...)
Observers are notified when there sequence completes/fails through
#onCompleted()/#onError()
Creating an Observable
● Create an Observable using #create(...)
Creating an Observable
● Create an Observable from item/s using #just(...)
● Create an Observable from an Iterable using #from(...)
● Create an Observable that emits items given an interval using #interval(...)
Creating an Observable (cont’d)
● Defer creation of Observable until subscription
Subscribing to an Observable
● Observer has #onNext(...), #onCompleted(...), and #onError(...)
Unsubscribing to an Observable
● Observable#subscribe(...) returns a Subscription object from which the
caller can invoke Subscription#unsubscribe()
Operator
● Most powerful part about Observables is that you can transform them and
perform functional style programming—map(), debounce(), filter(), etc.
● Transform Observable instances through Operators
Operator
Transforming an Observable
● Transform values emitted by Observable using the #map(...) operator
Filtering an Observable
● Filter values emitted by Observable using #filter(...) operator
Scheduling an Observable
● Specify a Scheduler where the Observable should operate using #subscribeOn(...),
specify a Scheduler where the Observable should notify its observers using
#observeOn(...)
Error Handling
● Re-subscribe/retry when the Observable emits an error
Ex. Double Clicks
Stream of clicks
Accumulate clicks in a list
Get length of list
Filter
Ex. Double Clicks
Simulate a stream of clicks:
Ex. Double Clicks
Accumulate clicks until 250 ms has passed between clicks:
Ex. Double Clicks
Map the length of each list & filter for sizes >= 2:
GitHub Example
● Interact with GitHub’s API using Retrofit and RxJava
○ https://github.com/arriolac/GithubRxJava
Questions??
Thank You!
Twitter: @arriolachris

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Functional programming
Functional programmingFunctional programming
Functional programming
 
Java 8 streams
Java 8 streams Java 8 streams
Java 8 streams
 
Linq
LinqLinq
Linq
 
Effective java item 80 and 81
Effective java   item 80 and 81Effective java   item 80 and 81
Effective java item 80 and 81
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
 
Module 3: Introduction to LINQ (PowerPoint Slides)
Module 3: Introduction to LINQ (PowerPoint Slides)Module 3: Introduction to LINQ (PowerPoint Slides)
Module 3: Introduction to LINQ (PowerPoint Slides)
 
OCL 2.4. (... 2.5)
OCL 2.4. (... 2.5)OCL 2.4. (... 2.5)
OCL 2.4. (... 2.5)
 
Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesAsynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & Promises
 
At Last an OCL Debugger
At Last an OCL DebuggerAt Last an OCL Debugger
At Last an OCL Debugger
 
Link quries
Link quriesLink quries
Link quries
 
Functional Reactive Endpoints using Spring 5
Functional Reactive Endpoints using Spring 5Functional Reactive Endpoints using Spring 5
Functional Reactive Endpoints using Spring 5
 
Safe navigation in OCL
Safe navigation in OCLSafe navigation in OCL
Safe navigation in OCL
 
The OCLforUML Profile
The OCLforUML ProfileThe OCLforUML Profile
The OCLforUML Profile
 
Linq
LinqLinq
Linq
 
Callback Function
Callback FunctionCallback Function
Callback Function
 
JSAnkara Swift v React Native
JSAnkara Swift v React NativeJSAnkara Swift v React Native
JSAnkara Swift v React Native
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
 
Java 8 Streams
Java 8 StreamsJava 8 Streams
Java 8 Streams
 
LINQ
LINQLINQ
LINQ
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 

Semelhante a Introduction to RxJava on Android

Reactive programming
Reactive programmingReactive programming
Reactive programming
BeauLiu
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
jeffz
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)
jeffz
 

Semelhante a Introduction to RxJava on Android (20)

响应式编程及框架
响应式编程及框架响应式编程及框架
响应式编程及框架
 
Introduction to Reactive programming
Introduction to Reactive programmingIntroduction to Reactive programming
Introduction to Reactive programming
 
RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015RxJava for Android - GDG DevFest Ukraine 2015
RxJava for Android - GDG DevFest Ukraine 2015
 
ReactiveCocoa in Practice
ReactiveCocoa in PracticeReactiveCocoa in Practice
ReactiveCocoa in Practice
 
Reactive programming
Reactive programmingReactive programming
Reactive programming
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
 
Angular 16 – the rise of Signals
Angular 16 – the rise of SignalsAngular 16 – the rise of Signals
Angular 16 – the rise of Signals
 
Reactive Streams and RxJava2
Reactive Streams and RxJava2Reactive Streams and RxJava2
Reactive Streams and RxJava2
 
Integration-Monday-Stateful-Programming-Models-Serverless-Functions
Integration-Monday-Stateful-Programming-Models-Serverless-FunctionsIntegration-Monday-Stateful-Programming-Models-Serverless-Functions
Integration-Monday-Stateful-Programming-Models-Serverless-Functions
 
Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaReactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJava
 
Reactive Thinking in iOS Development - Pedro Piñera Buendía - Codemotion Amst...
Reactive Thinking in iOS Development - Pedro Piñera Buendía - Codemotion Amst...Reactive Thinking in iOS Development - Pedro Piñera Buendía - Codemotion Amst...
Reactive Thinking in iOS Development - Pedro Piñera Buendía - Codemotion Amst...
 
Observables in Angular
Observables in AngularObservables in Angular
Observables in Angular
 
Workshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte IIWorkshop 13: AngularJS Parte II
Workshop 13: AngularJS Parte II
 
Reactive systems
Reactive systemsReactive systems
Reactive systems
 
Reactive Java (GeeCON 2014)
Reactive Java (GeeCON 2014)Reactive Java (GeeCON 2014)
Reactive Java (GeeCON 2014)
 
Understanding the Android System Server
Understanding the Android System ServerUnderstanding the Android System Server
Understanding the Android System Server
 
Concurrecny inf sharp
Concurrecny inf sharpConcurrecny inf sharp
Concurrecny inf sharp
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)
 
Reactive Programming no Android
Reactive Programming no AndroidReactive Programming no Android
Reactive Programming no Android
 
Nicholas Gustilo "Clean Android: building great mobile apps"
Nicholas Gustilo "Clean Android: building great mobile apps"Nicholas Gustilo "Clean Android: building great mobile apps"
Nicholas Gustilo "Clean Android: building great mobile apps"
 

Último

AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Último (20)

UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 

Introduction to RxJava on Android

Notas do Editor

  1. About this talk What: RxJava basics RxJava + Android + Coding towards the end What Not: Not a talk about functional programming Not a talk about in depths of RxJava, goal is to pique your interest in integrating RxJava in your project About me
  2. Concurrency - #1 reason. mobile is inherently concurrently. Best abstractions around dealing with concurrency Concise - very easy to follow the flow and transformation of data Defensive programming - very simple constructs that make dealing with various edge cases easier Code behave in a more predictable manner Level of abstraction - the reason why it provides all these benefits
  3. Callback hell Poor readability - caused by deep nesting Error prone Failing in between When it comes to readability you want to think of this as a tree data structure
  4. 1. Removes the need for callbacks End result is all in one place (Action1) Error handling is all in one place (doOnError) 2. Improved readability 3. Operation is a sequence of stream transformations
  5. .NET Reactive Extension - 2009, RxJava - 2012. Functional Reactive Programming Everything is a stream. If you are familiar with Futures, Iterables, and Observer - it’s a combination of those
  6. 3 concepts that are the foundation of RxJava
  7. The RxJava representation of a stream Atomic unit in Rx Something that can be observed
  8. Looks a lot like the Gang of Four Observer design pattern. The main difference is that Observables only start emitting once a subscriber subscribes.
  9. Pass in a class that implements OnSubscribe()
  10. Convenience methods for creating an Observable Finite vs. infinite stream
  11. Defer execution until subscription - this guarantees that the factory passed in #defer() only executes when #subscribe(...) is invoked. Ideal with long operations
  12. What gets logged in this example? Observer/Subscriber are sometimes used interchangeably. The Observer is the interface and a Subscriber is an implementation of an Observer
  13. Unsubscribe whenever the observer is no longer interested in the result
  14. Observable and Observer are independent of the transformational steps that occur in between A dozen operators that exist and we won’t be talking about all of them but I’ll mention a few that I think are commonly used
  15. ++ documentation Operators act in between emissions by Observables and receives by Observers
  16. Rxjava has a huge collection of Operators but I want to focus on some really common operators Observables provide a fluid interface
  17. Can’t you just surround with if statement? Goes back to defensive programming
  18. Think of Scheduler as an Executor By default, an Observable and the chain of operators that you apply to it will do its work, and will notify its observers, on the same thread on which its Subscribe method is called
  19. Network call
  20. Click every 250ms, add a delay of [0, 250]ms between clicks
  21. Using the Observable creation methods we mentioned earlier, the created Observable is considered a cold observable publish().refCount() turns this Observable to a hot Observable
  22. Done with talk on RxJava -> Now talk about integrating RxJava in Android One of the best applications of RxJava is for networking RxJava + Retrofit