RxBus的實現(xiàn)及簡單使用

原文鏈接:https://lingyunzhu.github.io

RxJava目前已經(jīng)很火了,如果你尚未了解請看這里。對于RxJava這里不多做介紹。
RxBus并不是一個庫,而是一種模式。相信大多數(shù)開發(fā)者都使用過EventBus,作為事件總線通信庫,如果你的項目已經(jīng)加入RxJava和EventBus,不妨用RxBus代替EventBus,以減少庫的依賴。

一、添加RxJava和RxAndroid依賴

    //RxJava and RxAndroid
    compile 'io.reactivex:rxandroid:1.1.0'
    compile 'io.reactivex:rxjava:1.1.0'

二、新建RxBus類

不多說直接上代碼:

import rx.Observable;
import rx.subjects.PublishSubject;
import rx.subjects.SerializedSubject;
import rx.subjects.Subject;

/**
 * Created by xialo on 2016/6/28.
 */
public class RxBus {

    private static volatile RxBus mInstance;

    private final Subject bus;


    public RxBus()
    {
        bus = new SerializedSubject<>(PublishSubject.create());
    }

    /**
     * 單例模式RxBus
     *
     * @return
     */
    public static RxBus getInstance()
    {

        RxBus rxBus2 = mInstance;
        if (mInstance == null)
        {
            synchronized (RxBus.class)
            {
                rxBus2 = mInstance;
                if (mInstance == null)
                {
                    rxBus2 = new RxBus();
                    mInstance = rxBus2;
                }
            }
        }

        return rxBus2;
    }


    /**
     * 發(fā)送消息
     *
     * @param object
     */
    public void post(Object object)
    {

        bus.onNext(object);

    }

    /**
     * 接收消息
     *
     * @param eventType
     * @param <T>
     * @return
     */
    public <T> Observable<T> toObserverable(Class<T> eventType)
    {
        return bus.ofType(eventType);
    }
}

1、Subject同時充當了Observer和Observable的角色,Subject是非線程安全的,要避免該問題,需要將 Subject轉(zhuǎn)換為一個 SerializedSubject,上述RxBus類中把線程非安全的PublishSubject包裝成線程安全的Subject。
2、PublishSubject只會把在訂閱發(fā)生的時間點之后來自原始Observable的數(shù)據(jù)發(fā)射給觀察者。
3、ofType操作符只發(fā)射指定類型的數(shù)據(jù),其內(nèi)部就是filter+cast

三、創(chuàng)建你需要發(fā)送的事件類

我們這里用StudentEvent舉例

/**
 * Created by xialo on 2016/6/28.
 */
public class StudentEvent {
    private String id;
    private String name;

    public StudentEvent(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

四、發(fā)送事件

RxBus.getInstance().post(new StudentEvent("007","小明"));

五、接收事件

rxSbscription=RxBus.getInstance().toObserverable(StudentEvent.class)
                .subscribe(new Action1<StudentEvent>() {
                    @Override
                    public void call(StudentEvent studentEvent) {
                        textView.setText("id:"+ studentEvent.getId()+"  name:"+ studentEvent.getName());
                    }
                });

注:rxSbscription是Sbscription的對象,我們這里把RxBus.getInstance().toObserverable(StudentEvent.class)賦值給rxSbscription以方便生命周期結(jié)束時取消訂閱事件

六、取消訂閱

@Override
    protected void onDestroy() {
        if (!rxSbscription.isUnsubscribed()){
            rxSbscription.unsubscribe();
        }
        super.onDestroy();
    }

參考:
http://wuxiaolong.me/2016/04/07/rxbus/
http://www.lxweimin.com/p/ca090f6e2fe2

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容