RxJava makes dealing with concurrency and asynchronous operations easier by allowing data streams to be combined, transformed, filtered and mapped. It encourages defensive programming and simplifies error handling. RxJava represents asynchronous data streams and allows these streams to be composed through the use of operators. Observables emit items to observers, and operators allow transforming the streams by mapping, filtering, debouncing etc. This introduces a more functional reactive approach to programming with asynchronous data.
1. Intro to RxJava on Android
Chris Arriola
Functional Reactive Programming
2. 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?
5. 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
7. 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
8. 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()
10. 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(...)
12. Subscribing to an Observable
● Observer has #onNext(...), #onCompleted(...), and #onError(...)
13. Unsubscribing to an Observable
● Observable#subscribe(...) returns a Subscription object from which the
caller can invoke Subscription#unsubscribe()
14. 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
18. 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(...)
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
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
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
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
.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
3 concepts that are the foundation of RxJava
The RxJava representation of a stream
Atomic unit in Rx
Something that can be observed
Looks a lot like the Gang of Four Observer design pattern. The main difference is that Observables only start emitting once a subscriber subscribes.
Pass in a class that implements OnSubscribe()
Convenience methods for creating an Observable
Finite vs. infinite stream
Defer execution until subscription - this guarantees that the factory passed in #defer() only executes when #subscribe(...) is invoked.
Ideal with long operations
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
Unsubscribe whenever the observer is no longer interested in the result
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
++ documentation
Operators act in between emissions by Observables and receives by Observers
Rxjava has a huge collection of Operators but I want to focus on some really common operators
Observables provide a fluid interface
Can’t you just surround with if statement?
Goes back to defensive programming
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
Network call
Click every 250ms, add a delay of [0, 250]ms between clicks
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
Done with talk on RxJava -> Now talk about integrating RxJava in Android
One of the best applications of RxJava is for networking
RxJava + Retrofit