For most of us, Reactive Android means using RxJava. In this presentation, I try to borrow a few ideas from the backend world and enrich the concept of Reactive in Android.
10. RxJava goes hand in hand with Java8’s Lambdas
new Func1<String, Integer>() {
@Override
public Integer call(String s) {
return s.length();
}
}
(String s) -> {
return s.length();
}
s -> s.length();
Retrolamba plugin for
Android < N
11. RxJava: dealing with a stream of items
class Cat {
...
public Collar getCollar() {…}
public Picture fetchPicture() {…}
...
}
12. RxJava: dealing with a stream of items
Observable.from(myCats);
(cat -> Log.d(TAG, “got cat”));
List<Cat> myCats;
Observable obs =
obs.subscribe
13. RxJava: dealing with a stream of items
Observable.from(myCats)
.subscribe(cat -> Log.d(TAG, “got cat”));
List<Cat> myCats;
14. RxJava: work on the stream
Observable.from(myCats)
.map(cat -> cat.getCollar())
.subscribe(collar -> Log.d(TAG, “got collar”));
map: T -> R
(this map: Cat -> Collar)
15. RxJava: operators to manipulate the stream
Observable.from(myCats)
.distinct()
.delay(2, TimeUnit.SECONDS)
.filter(cat -> cat.isWhite())
.subscribe(cat -> Log.d(TAG, “got white cat”));
19. RxJava: other ways of creating Observables
// emits on single item and completes
Observable.just
// emits one item after a certain delay
Observable.timer
.. plus many others, and you can create your own!
20. RxJava: demo with Retrofit & Meetup Streams
http://stream.meetup.com/2/rsvp
Meetup Streaming API
25. RxJava: demo with Meetup Streams
https://github.com/ticofab/android-meetup-streams
26. Reactive in (Android) frontend VS backend
ANDROID
Using RxJava
WHAT DO PEOPLE THINK THAT
“BEING REACTIVE” MEANS?
BACKEND
?
27. Evolution of server applications & user expectations
2006 2016
Servers ~10 The sky is the limit.
Response time seconds milliseconds
Offline maintenance hours what?
Data amount Gigabytes Petabytes
Machines Single core, little distribution
Must work across async
boundaries (location, threads)
Kind of data Request - response Streams (endless)
29. The Reactive traits
A reactive computer system must Trait
React to its users Responsive
React to failure and stay available Resilient
React to varying load conditions Elastic
Its components must react to inputs Message-driven
31. Reactive traits: Responsive
• A human who visits a website
• A client which makes a request to a server
• A consumer which contacts a provider
• . . .
A Reactive system responds to inputs and usage from its user.
32. Reactive traits: Elastic
• Scale OUT and IN: use just the right amount
• Elasticity relies on distribution
• Ensure replication in case of failure
Scale on demand to react to varying load
38. Reactive Pattern: Simple Component Pattern
“One component should do only one thing but do it in full. The aim is to
maximise cohesion and minimise coupling between components.”
39. Reactive Pattern: Simple Component Pattern
Actor 1
Actor 3
Actor 2
• contains state
• has a mailbox to receive
and send messages
• contains behaviour logic
• has a supervisor
Actor model Supervisor
40. Reactive Patterns: Let it crash!
"Prefer a full component restart to complex internal failure handling".
• failure conditions WILL occur
• they might be rare and hard to reproduce
• it is much better to start clean than to try to recover
• …which might be expensive and difficult!
41.
42. Reactive Patterns: Let it crash!
• Components should be isolated - state is not shared
• Components should have a supervisor and delegate to it
some or all error handling
• The supervisor can transparently restart the component
• Message-passing architectures enforce state
confinement, error containment and transparency
In practice…
43. Reactive Patterns: Let it crash!
Actor supervision example
Actor 1
Actor 2
Supervisor
WhateverException!
X
Fix or
Restart
50. Reactive traits in Android?
Can we apply some of the Reactive Manifesto
principles to our Android development?
51. Reactive traits in Android?
Reactive trait In Android?
Responsive Execute as much as possible asynchronously
Elastic —
Resilient Delegate risky stuff to (Intent)Services or isolated components
Message passing
Communication via ResultReceiver
Use some event Bus
52. Reactive traits in Android?
…but I am sure that we can take this further.