RxBus的实现及简单使用

原文连接:http://lingyunzhu.github.iohtml

RxJava目前已经很火了,若是你还没有了解请看这里。对于RxJava这里很少作介绍。
RxBus并非一个库,而是一种模式。相信大多数开发者都使用过EventBus,做为事件总线通讯库,若是你的项目已经加入RxJava和EventBus,不妨用RxBus代替EventBus,以减小库的依赖。java

1、添加RxJava和RxAndroid依赖

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

2、新建RxBus类

很少说直接上代码:react

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;
    }


    /**
     * 发送消息
     *
     * @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);
    }
}

一、Subject同时充当了Observer和Observable的角色,Subject是非线程安全的,要避免该问题,须要将 Subject转换为一个 SerializedSubject,上述RxBus类中把线程非安全的PublishSubject包装成线程安全的Subject。
二、PublishSubject只会把在订阅发生的时间点以后来自原始Observable的数据发射给观察者。
三、ofType操做符只发射指定类型的数据,其内部就是filter+castandroid

3、建立你须要发送的事件类

咱们这里用StudentEvent举例git

/**
 * 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;
    }
}

4、发送事件

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

5、接收事件

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以方便生命周期结束时取消订阅事件github

6、取消订阅

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

参考:
http://wuxiaolong.me/2016/04/...安全

相关文章
相关标签/搜索