Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Reactive Android 101

This is the first 75-minute half of my presentation on using reactive programming at AnDevCon Boston 2015. -- @colinmlee
by

Colin Lee

on 2 August 2016

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Reactive Android 101

Intro to Reactive Android
@colinmlee
How Did Reactive Happen?
Functional Reactive Programming
Invented in 1997
Started in academic languages
Represents data as how values change over time
Event streams > State
Reactive Extensions
Created by Microsoft's Erik Meijer for C#.NET
Rx.NET brought the best ideas of FRP to a highly productive, business language
RxJava
Created by Ben Christenson and his team at Netflix in 2013 to improve the resiliency of their networked services
Inspired by Rx.NET, Netflix provided a full port of reactive extensions for several JVM languages
Gang of Four
Four programmers publish a list of the most useful design patterns in 1994.

The Observer pattern becomes one of their most successful patterns.
Who IS This Guy?
Colin Lee
Independent Android Engineer
Programming since age six
Races cars for fun
Electric auto enthusiast
@colinmlee
Observables
Differences from Observer Pattern
Offers composable functions to manipulate event streams
An "Iterable Future"
Synchronous
Asynchronous
Single Item
Multiple Items
T
Future<T>
Iterable<T>
Observable<T>
Observables fill the gap by being the ideal way to access asynchronous sequences of multiple items
Do We Write Business Logic or Maintain State?
Observables offer an alternative to state variables.
Escape From Callback Hell
Deeply nested callbacks are hard to read and understand.
Named functions make callback hell worse.
Callbacks considered harmful.
Who Uses This?
"Netflix is a big believer in the Rx model, because Rx has made it much easier for us to build complex asynchronous programs.

RxJava is effective on the server for us and it spreads deeper into our code the more we use it."
"As things become more asynchronous, the functional model really fits well.

The ability to chain and transform async operations with hassle-free error propagation can be a powerful tool in any developer’s arsenal."
"Using RxJava to replace loaders and internal lib to replace fragments/activities. All that's left is views, android.animation.*, and bliss."
"Both our Android and iOS teams use the reactive programming paradigm to simplify asynchronous, concurrent code in our native mobile apps.

At SoundCloud, we are moving most of our code that relies heavily on event-based and asynchronous operations to Rx observables."
The Problem
More Threads
More Cloud
Networked Threading Gets Messy
Locking
Error Recovery
Synchronization
Deadlocks
Android Lifecycle
Retry
Caching
Sharing State
Callback Hell
Negative Testing
AsyncTask is a Hack
Cancellation broken
Memory leaks common
Breaks on lifecycle events
Poor thread safety
Broken error handling
Convoluted logic for parallelism
If code gets subscribed to at the end of a chain, we know what came earlier.
Subjects
What is a Subject?
Sometimes you want an Observable that is also an Observer. Input as well as output.
Subjects are useful for double binding models with views, as event buses, and for wrapping Observables.
Subject Downsides
Subjects break the Functional Programming contract with side effects, so they are best avoided when an Observable would also work.
When they're needed, they are really useful.
Breaking the Contract
When observing Subjects, they should always be cast to Observables.
A Subject's observers should never modify a Subject because to do so is evil. It tears up the contract.
Four Subject Behaviors
AsyncSubject
: Emits final value only after source Observable completes.
BehaviorSubject
: Emits most recent value when subscribed then each value afterwards.
PublishSubject
: Emits each value that occurs after subscription.
ReplaySubject
: Emits every value that occurred before subscription and then each value after.
Key Operators
map()
Transform the Observable's type by applying a function.
flatMap()
Transform the items emitted by an Observable into Observables, then flatten this into a single Observable.
filter()
Filter items emitted by an Observable using a function which returns a boolean value.
take()
Emit only the first n items.
RxJava Examples
Observables
Questions?
Please take a moment to fill out
the class feedback form via the
app. Paper feedback forms are
also available in the back of the
room.
All examples are on Github at:

https://github.com/colintheshots
Observables
Combining Streams
It's easy to combine and chain observables.
Why Functional?
The chain avoids race conditions and guarantees state is accurate.
Marble Diagrams
Handles errors
Handles completion
Each marble represents an event.
Each line represents an observable.
Marble Diagrams
Time flows to the right.
The chain flows down.
http://eventmobi.com/adcboston16
Full transcript