ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
REACTIVE YOUR ANDROID
Fernando Franco Gir¨¢ldez
@thanerian
ffrancogiraldez@gmail.com
The world has moved to push; users are waiting for
us to catch up
Lee Campbell
Introduction to Rx
Motivation
Event Bus
Bus bus;!
!
public void onCreate(Bundle arg) {!
super.onCreate(arg);!
//Subscription!
bus.register(this);!
}!
!
public void onDestroy() {!
super.onDestroy();!
bus.unregister(this);!
}!
!
@Subscribe!
public void answerAvailable(!
WeatherEvent event!
) {!
// TODO: React to the event somehow!!
}!
!
// in other class!
@Produce!
public WeatherEvent produceWeather() {!
return new WeatherEvent(!
Weather.COLD!
);!
}!
!
?? Hidden dependencies
?? Implicit subscription
?? Composition
?? Concurrency
?? Error handling
DEMO
EVENTS ARE CLUNKY
Reactive Extensions
?? Extends Observer Pattern
?? It¡¯s a library for composing asynchronous and event-
based programs by using observable secuences.
?? Observable
?? Subscriber
?? Subscription
Core elements
Subscriber
!
new SubscriberLong() {!
@Override!
public void onCompleted() {!
!
}!
!
@Override!
public void onError(Throwable e) {!
!
}!
!
@Override!
public void onNext(Long aLong) {!
!
}!
};!
!
!
Observable
ObservableLong timeObservable = Observable!
.create(new Observable.OnSubscribeLong() {!
@Override!
public void call(Subscriber? super Long subscriber) {!
try {!
subscriber.onNext(System.currentTimeMillis());!
} catch (Exception ex) {!
subscriber.onError(ex);!
} finally {!
subscriber.onCompleted();!
}!
}!
}!
!
);!
Subscription
Subscription subscription = !
timeObservable.subscribe(new Action1Long() {!
@Override!
public void call(Long aLong) {!
!
}!
});!
!
subscription.unsubscribe();!
DEMO
SIMPLE WITH LAMBDAS
?? Observable.just()
?? Observable.never()
?? Observable.interval()
?? RxTextView.texts()
?? RxView.clicks()
Creaci¨®n de Observables
DEMO
EVENTS THROTTLING
Operadores
public ObservableTextMessage !
fetchTextMessage(String ofNumber){!
ListTextMessage messageList = !
eventRepository.find(ofNumber);!
return Observable.from(messageList)!
.concatWith(!
runtimeEvents.ofType(TextMessage.class)!
) .filter(message-!
Objects.equals(message.number(), ofNumber)!
);!
}!
!
DEMO 
OPERATORS
OBSERVABLE
SYNCRONIZATION
Schedulers
timeObservable!
.subscribeOn(Schedulers.io())!
.observeOn(AndroidSchedulers.mainThread())!
.subscribe(new Action1Long() {!
@Override!
public void call(Long aLong) {!
!
}!
});!
To add concurrency into our observable secuence, just indicate
to the observable to operate into a particular Scheduler
https://channel9.msdn.com/Series/Rx-Workshop/Rx-Workshop-Schedulers
Schedulers
timeObservable!
.subscribeOn(Schedulers.io())!
.observeOn(AndroidSchedulers.mainThread())!
.subscribe(new Action1Long() {!
@Override!
public void call(Long aLong) {!
!
}!
});!
Specify the Scheduler on which an Observavble will operate
Schedulers
timeObservable!
.subscribeOn(Schedulers.io())!
.observeOn(AndroidSchedulers.mainThread())!
.subscribe(new Action1Long() {!
@Override!
public void call(Long aLong) {!
!
}!
});!
Specify the Scheduler on wich an subscriber will observe this
Observable
DEMO
CONCURRENCY
?? Schedulers.inmediate()
?? Schedulers.newThread()
?? Schedulers.io()
?? AndroidSchedulers.mainThread()
Schedulers
?? ?How to provide Schedulers?
?? ?How to make test run faster?
?? TestScheduler
Testing and Rx
DEMO
TESTING
?? Semantic ?uid syntax
?? Composition and synchronization made easy
?? Multiplatform
?? High impat learning curve
Conclusions
?Q  A?
?? MSDN https://msdn.microsoft.com/en-us/data/gg577609.aspx
?? Reactivex.io http://reactivex.io/tutorials.html
?? Intro to Rx (ebook) http://www.introtorx.com
?? RxMarbles rxmarbles.com
?? Reactive Streams http://www.reactive-streams.org/
Readings
?? Android Clean Architecture-
https://github.com/android10/Android-CleanArchitecture
?? hexagonal-mvp-reactive -
https://github.com/ffgiraldez/hexagonal-mvp-reactive-android
?? rx-architecture - https://github.com/tehmou/rx-android-architecture
?? Android-RxJava - https://github.com/kaushikgopal/RxJava-Android-Samples
?? RxAndroid - https://github.com/ReactiveX/RxAndroid/
Source code

More Related Content

Reactive your android