【Potatotips #26】Replace EventBus with RxJava/RxAndroid
-
Upload
hiroyuki-kusu -
Category
Technology
-
view
2.378 -
download
1
Transcript of 【Potatotips #26】Replace EventBus with RxJava/RxAndroid
Replace EventBus with RxJava/RxAndroid
Hiroyuki Kusu ( @hkusu_ )株式会社ゆめみ
2016/02/17 potatotips #26
Android 以外も色々やります。最近は主に JavaScript( Node.js + ES2015 ) を書いてます。
greenrobot/EventBus
⇒ Replace with RxJava(RxAndroid)
app/build.gradledependencies { // ... compile 'io.reactivex:rxjava:1.1.0' compile 'io.reactivex:rxandroid:1.1.0'}
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
public class RxEventBusProvider { private static final RxEventBus rxEventBus = new RxEventBus();
public static RxEventBus provide(){ return rxEventBus; }}
RxEventBusProvider.java
※ providing the singleton instance
RxEventBus rxEventBus = RxEventBusProvider.provide();
rxEventBus.post(new ChangedEvent());
rxEventBus.onEvent(ChangedEvent.class, event -> { // something..});
※ applying Java8 & Retrolambda
Getting instance
Publish
Subscribe
Subscription subscription;
// ...
subscription = rxEventBus.onEvent(ChangedEvent.class, event -> { // something..});
// ...
subscription.unsubscribe();
Unsubscribe ( order not to leak)
Tips
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
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
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).
Appendix
Providing a singleton instancein Dagger2
@Modulepublic class AppModule { @Provides @Singleton public RxEventBus provideRxEventBus(){ return new RxEventBus(); }}
AppModule.java
@Component(modules = AppModule.class)public interface AppComponent { RxEventBus provideRxEventBus();}
AppComponent.java
Getting instanceAppComponent 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 .
Sample code
hkusu/android-dagger-rxjava-sample
Thanks!