Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reactive your Android

Reactive your Android

Want to use your Rx in your Android project and do not know how?

On this talk will review the most common elements when working with Rx and how they are interconnected with a practical approach. We see cases of practical uses of some commonly operators and how we can avoid AsyncTask and Event Bus speeding development.

Fernando Franco Gíraldez

November 07, 2015
Tweet

More Decks by Fernando Franco Gíraldez

Other Decks in Programming

Transcript

  1. The world has moved to push; users are waiting for

    us to catch up Lee Campbell Introduction to Rx Motivation
  2. 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
  3. Reactive Extensions •  Extends Observer Pattern •  It’s a library

    for composing asynchronous and event- based programs by using observable secuences.
  4. Subscriber ! new Subscriber<Long>() {! @Override! public void onCompleted() {!

    ! }! ! @Override! public void onError(Throwable e) {! ! }! ! @Override! public void onNext(Long aLong) {! ! }! };! ! !
  5. 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();! }! }! }! ! );!
  6. 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)! );! }! !
  7. 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
  8. •  ¿How to provide Schedulers? •  ¿How to make test

    run faster? •  TestScheduler Testing and Rx
  9. •  Semantic fluid syntax •  Composition and synchronization made easy

    •  Multiplatform •  High impat learning curve Conclusions
  10. •  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
  11. •  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