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.gradle
dependencies {
// ...
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 instance
in Dagger2
@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
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 .
Sample code
hkusu/android-dagger-rxjava-sample
Thanks!

【Potatotips #26】Replace EventBus with RxJava/RxAndroid

  • 1.
    Replace EventBus with RxJava/RxAndroid HiroyukiKusu ( @hkusu_ ) 株式会社ゆめみ 2016/02/17 potatotips #26
  • 2.
  • 3.
  • 4.
    app/build.gradle dependencies { // ... compile'io.reactivex:rxjava:1.1.0' compile 'io.reactivex:rxandroid:1.1.0' }
  • 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.
    public class RxEventBusProvider{ private static final RxEventBus rxEventBus = new RxEventBus(); public static RxEventBus provide(){ return rxEventBus; } } RxEventBusProvider.java ※ providing the singleton instance
  • 7.
    RxEventBus rxEventBus =RxEventBusProvider.provide(); rxEventBus.post(new ChangedEvent()); rxEventBus.onEvent(ChangedEvent.class, event -> { // something.. }); ※ applying Java8 & Retrolambda Getting instance Publish Subscribe
  • 8.
    Subscription subscription; // ... subscription= rxEventBus.onEvent(ChangedEvent.class, event -> { // something.. }); // ... subscription.unsubscribe(); Unsubscribe ( order not to leak)
  • 9.
  • 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.
    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.
    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.
    Appendix Providing a singletoninstance in Dagger2
  • 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.
    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.
  • 17.