前面说过Rxjava的功能很强大,不仅仅是实现链式的异步操作,它的功能很强大还可以通过RxBus实现EventBus的消息/事件传递功
能,我们来看看
RxBus
package com.example.liujian.rxjavademo; import java.util.HashMap; import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;
import rx.subjects.SerializedSubject;
import rx.subscriptions.CompositeSubscription; /**
* @project_Name: RxjavaDemo
* @package: com.example.liujian.rxjavademo
* @description: 使用Rxjava,RxAndroid实现RxBus
* @author: liujian
* @date: 2016/10/5 11:08
* @version: V1.0
*/ public class RxBus {
private static volatile RxBus mInstance;
//Subject继承了Observable类又实现了Observer接口, Subject可以同时担当订阅者和被订阅者的角色
private SerializedSubject<Object,Object> mSubject;
//一个类产生多个Subscription对象,用一CompositeSubscription 存储起来,以进行批量的取消订阅。避免内存泄漏
private HashMap<String,CompositeSubscription> mSubscriptionHashMap;
private RxBus(){
//Subject是非线程安全的,SerializedSubject将PublishSubject 转换成一个线程安全的Subject对象
mSubject=new SerializedSubject<>(PublishSubject.create());
}
public static RxBus getInstance(){
if(mInstance==null){
synchronized (RxBus.class){
if(mInstance==null){
mInstance=new RxBus();
}
}
}
return mInstance;
}
/**
* 发生消息
*/
public void post(Object o){
mSubject.onNext(o);
} /**
* 返回指定类型的Observable实例
* @param type:要处理的消息的类型
* @param <T>
* @return
*/
public <T>Observable<T> toObservable(final Class<T> type){
return mSubject.ofType(type);
} /**
* 是否已含有观察者订阅
* @return
*/
public boolean hasObservers(){
return mSubject.hasObservers();
} /**
* 默认的订阅方法
* @param <T>
* @return
*/
public <T>Subscription doSubscribe(Class<T> type, Action1<T> next){
return toObservable(type)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(next);
}
/**
* 默认的订阅方法
* @param <T>
* @return
*/
public <T>Subscription doSubscribe(Class<T> type, Action1<T> next,Action1<Throwable> error){
return toObservable(type)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(next,error);
}
/**
* 默认的订阅方法
* @param <T>
* @return
*/
public <T>Subscription doSubscribe(Class<T> type, Action1<T> next, Action1<Throwable> error, Action0 complete){
return toObservable(type)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(next,error,complete);
} /**
* 保存订阅后的subscription,方便一次性取消订阅
* @param o
* @param subscription
*/
public void addSubscription(Object o,Subscription subscription){
if(mSubscriptionHashMap==null){
mSubscriptionHashMap=new HashMap<>();
}
String key=o.getClass().getSimpleName();
if(mSubscriptionHashMap.containsKey(key)){
mSubscriptionHashMap.get(key).add(subscription);
}else{
CompositeSubscription compositeSubscription=new CompositeSubscription();
compositeSubscription.add(subscription);
mSubscriptionHashMap.put(key,compositeSubscription);
}
} /**
* 取消订阅
* @param o
*/
public void unSubscribe(Object o){
if(mSubscriptionHashMap==null){
return;
}
String key=o.getClass().getSimpleName();
if(!mSubscriptionHashMap.containsKey(key)){
return;
}
if(mSubscriptionHashMap.get(key)!=null){
mSubscriptionHashMap.get(key).unsubscribe();
}
mSubscriptionHashMap.remove(key);
}
}
不多解释了,注释都写的很清楚了,我们来看一下如何使用
发送消息:
RxBus.getInstance().post("这是发送的消息");
处理消息:
public void doSubscribe(){
Subscription subscription = RxBus.getInstance().doSubscribe(String.class, new Action1<String>() {
@Override
public void call(String s) {
Toast.makeText(MainActivity.this,s, Toast.LENGTH_SHORT).show();
}
});
RxBus.getInstance().addSubscription(this,subscription);
}
或者
public void doSubscribe(){
Subscription subscribe = RxBus.getInstance().toObservable(String.class)
.filter(new Func1<String, Boolean>() {
@Override
public Boolean call(String s) {
return true;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
Log.i(TAG, "call: " + s);
}
});
RxBus.getInstance().addSubscription(this,subscribe);
}
取消订阅:
@Override
protected void onDestroy() {
//取消订阅,释放内存
RxBus.getInstance().unSubscribe(this);
super.onDestroy();
}
功能的完善
当我们使用PublishSubject时,可能有些功能还不是很完善,比如我们只能先订阅事件,然后发送事件,如果反过来,先发送了事件再进
行订阅操作,比如两个Activity之间传递消息,怎么保证发送的事件不丢失呢?也就是EventBus的StickEvent功能,这个时候
PublishSubject就没有办法实现,我们可以替换为BehaviorSubject
private RxBus() {
mSubject = new SerializedSubject<>(BehaviorSubject.create());
}
但是BehaviorSubject只能缓存最近一个发送给它的事件,如果我们需要缓存多个事件可以改用ReplaySubject