【Potatotips #26】Replace EventBus with RxJava/RxAndroid

2,368 views

Published on

2016/02/17(Wed)に開催された potatotips #26 (http://connpass.com/event/25401/) での発表資料です。

Published in: Technology

【Potatotips #26】Replace EventBus with RxJava/RxAndroid

  1. 1. Replace EventBus with RxJava/RxAndroid Hiroyuki Kusu ( @hkusu_ ) 株式会社ゆめみ 2016/02/17 potatotips #26
  2. 2. Android 以外も色々やります。最近は主に JavaScript ( Node.js + ES2015 ) を書いてます。
  3. 3. greenrobot/EventBus ⇒ Replace with RxJava(RxAndroid)
  4. 4. app/build.gradle dependencies { // ... compile 'io.reactivex:rxjava:1.1.0' compile 'io.reactivex:rxandroid:1.1.0' }
  5. 5. public class RxEventBus { private final Subject<Object, Object> subject = new SerializedSubject<>(PublishSubject.create()); public <T> Subscription onEvent(Class<T> clazz, Action1<T> handler) { return subject .ofType(clazz) .subscribe(handler); } public void post(Object event) { subject.onNext(event); } } RxEventBus.java
  6. 6. public class RxEventBusProvider { private static final RxEventBus rxEventBus = new RxEventBus(); public static RxEventBus provide(){ return rxEventBus; } } RxEventBusProvider.java ※ providing the singleton instance
  7. 7. RxEventBus rxEventBus = RxEventBusProvider.provide(); rxEventBus.post(new ChangedEvent()); rxEventBus.onEvent(ChangedEvent.class, event -> { // something.. }); ※ applying Java8 & Retrolambda Getting instance Publish Subscribe
  8. 8. Subscription subscription; // ... subscription = rxEventBus.onEvent(ChangedEvent.class, event -> { // something.. }); // ... subscription.unsubscribe(); Unsubscribe ( order not to leak)
  9. 9. Tips
  10. 10. public class RxEventBus { private final Subject<Object, Object> subject = new SerializedSubject<>(PublishSubject.create()); public <T> Subscription onEvent(Class<T> clazz, Action1<T> handler) { return subject .ofType(clazz) .subscribe(handler); } public <T> Subscription onEventMainThread(Class<T> clazz, Action1<T> handler) { return subject .ofType(clazz) .observeOn(AndroidSchedulers.mainThread()) .subscribe(handler); } public void post(Object event) { subject.onNext(event); } } Subscribing in the main thread
  11. 11. public class ChangedEvent { private int id; public ChangedEvent(int id) { this.id = id; } public int getId() { return id; } } rxEventBus.post(new ChangedEvent(99)); rxEventBus.onEvent(ChangedEvent.class, event -> { int id = event.getId(); // something.. }); Getting a value from the event
  12. 12. rxEventBus.onEvent(TodoRepository.ChangedEvent.class, event -> { // something.. }); Create event as an inner class of the class to publish (or subscribe) the event. Because, it is unclear to post the event from where(or to where).
  13. 13. Appendix Providing a singleton instance in Dagger2
  14. 14. @Module public class AppModule { @Provides @Singleton public RxEventBus provideRxEventBus(){ return new RxEventBus(); } } AppModule.java @Component(modules = AppModule.class) public interface AppComponent { RxEventBus provideRxEventBus(); } AppComponent.java
  15. 15. Getting instance AppComponent appComponent = DaggerAppComponent.create(); // ... RxEventBus rxEventBus = appComponent.provideRxEventBus(); In this example, Dagger2 looks redundant , but redundant description can be reduced by @inject annotation to the constructor. See https://github.com/hkusu/android-dagger-rxjava-sample .
  16. 16. Sample code hkusu/android-dagger-rxjava-sample
  17. 17. Thanks!

×