Upload
fernando-franco-giraldez
View
103
Download
0
Embed Size (px)
Citation preview
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
Reactive Extensions
• Extends Observer Pattern
• It’s a library for composing asynchronous and event-based programs by using observable secuences.
Subscriber ! new Subscriber<Long>() { ! @Override ! public void onCompleted() { ! ! } ! ! @Override ! public void onError(Throwable e) { ! ! } ! ! @Override ! public void onNext(Long aLong) { ! ! } ! }; !!!
Observable
Observable<Long> timeObservable = Observable! .create(new Observable.OnSubscribe<Long>() { ! @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 Action1<Long>() {! @Override ! public void call(Long aLong) { !! } ! }); !! subscription.unsubscribe(); !
• Observable.just()
• Observable.never()
• Observable.interval()
• RxTextView.texts()
• RxView.clicks()
Creación de Observables
Operadores
public Observable<TextMessage> ! fetchTextMessage(String ofNumber){ ! List<TextMessage> messageList = ! eventRepository.find(ofNumber); ! return Observable.from(messageList) ! .concatWith( ! runtimeEvents.ofType(TextMessage.class) ! ) .filter(message-> ! Objects.equals(message.number(), ofNumber) ! ); ! } !
!
Schedulers
timeObservable ! .subscribeOn(Schedulers.io()) ! .observeOn(AndroidSchedulers.mainThread()) ! .subscribe(new Action1<Long>() {! @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 Action1<Long>() {! @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 Action1<Long>() {! @Override! public void call(Long aLong) {!! } ! }); !
Specify the Scheduler on wich an subscriber will observe this Observable
• Schedulers.inmediate()
• Schedulers.newThread()
• Schedulers.io()
• AndroidSchedulers.mainThread()
Schedulers
• Semantic fluid syntax
• Composition and synchronization made easy
• Multiplatform
• High impat learning curve
Conclusions
• 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