All Projects → Blankj → Rxbus

Blankj / Rxbus

Licence: apache-2.0
🚌 The RxBus as steady as an old dog.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Rxbus

ReactiveBus
🚍 Reactive Event Bus for JVM (1.7+) and Android apps built with RxJava 2
Stars: ✭ 17 (-94.91%)
Mutual labels:  rxjava, eventbus, rxandroid, rxjava2, rxandroid2
Reactivebeacons
Android library scanning BLE beacons nearby with RxJava
Stars: ✭ 171 (-48.8%)
Mutual labels:  rxjava, rxjava2, rxandroid, rxandroid2
Rxjavapriorityscheduler
RxPS - RxJavaPriorityScheduler - A RxJava Priority Scheduler library for Android and Java applications
Stars: ✭ 138 (-58.68%)
Mutual labels:  rxjava, rxjava2, rxandroid, rxandroid2
Rxbiometric
☝️ RxJava and RxKotlin bindings for Biometric Prompt (Fingerprint Scanner) on Android
Stars: ✭ 295 (-11.68%)
Mutual labels:  rxjava, rxjava2, rxandroid, rxandroid2
Reactivenetwork
Android library listening network connection state and Internet connectivity with RxJava Observables
Stars: ✭ 2,484 (+643.71%)
Mutual labels:  rxjava, rxjava2, rxandroid, rxandroid2
Prefser
Wrapper for Android SharedPreferences with object serialization and RxJava Observables
Stars: ✭ 228 (-31.74%)
Mutual labels:  rxjava, rxjava2, rxandroid, rxandroid2
Reactivewifi
Android library listening available WiFi Access Points and related information with RxJava Observables
Stars: ✭ 186 (-44.31%)
Mutual labels:  rxjava, rxjava2, rxandroid, rxandroid2
Swipe
👉 detects swipe events on Android
Stars: ✭ 324 (-2.99%)
Mutual labels:  rxjava, rxjava2, rxandroid, rxandroid2
Reactivesensors
Android library monitoring device hardware sensors with RxJava
Stars: ✭ 161 (-51.8%)
Mutual labels:  rxjava, rxjava2, rxandroid
Mvvm Architecture Android Beginners
This repository contains a sample app that implements MVVM architecture using Kotlin, ViewModel, LiveData, and etc.
Stars: ✭ 176 (-47.31%)
Mutual labels:  rxjava, rxjava2, rxandroid
Freezer
A simple & fluent Android ORM, how can it be easier ? RxJava2 compatible
Stars: ✭ 326 (-2.4%)
Mutual labels:  rxjava, rxjava2, rxandroid
Dagger2
Kotlin Dagger2 example project
Stars: ✭ 145 (-56.59%)
Mutual labels:  rxjava, rxjava2, rxandroid2
Mvpframes
整合大量主流开源项目并且可高度配置化的 Android MVP 快速集成框架,支持 AndroidX
Stars: ✭ 100 (-70.06%)
Mutual labels:  rxjava, rxjava2, rxandroid
Androidbasemvp
🚀一个快速搭建MVP+RxJava2+Retrofit 基础框架,主要是封装有Http网络请求、日志、缓存、加载等待、toast、页面状态布局管理、权限、RxBus、Glide图片加载等组件,方便快速开发新项目、减少开发成本。
Stars: ✭ 184 (-44.91%)
Mutual labels:  rxjava, rxjava2, rxandroid
Apollo
🚀 Awesome EventBus by RxJava.
Stars: ✭ 329 (-1.5%)
Mutual labels:  rxjava, rxjava2, eventbus
Rxbus
Event Bus By RxJava.
Stars: ✭ 2,126 (+536.53%)
Mutual labels:  rxjava, rxjava2, rxandroid
Android Clean Architecture Boilerplate
Apply clean architecture on Android
Stars: ✭ 141 (-57.78%)
Mutual labels:  rxjava, rxjava2, rxandroid
Aiyagirl
🔥 爱吖妹纸(含 Kotlin 分支版本)——Retrofit + RxJava + MVP 架构 APP 体验代码家的干货集中营 Gank.io,福利多多,不容错过
Stars: ✭ 1,109 (+232.04%)
Mutual labels:  rxjava, rxjava2, rxandroid
iMoney
iMoney 金融项目
Stars: ✭ 55 (-83.53%)
Mutual labels:  rxjava, rxjava2, rxandroid2
rxandroid2-retrofit2
Small tutorial to get started with RxAndroid 2 and Retrofit 2
Stars: ✭ 55 (-83.53%)
Mutual labels:  rxandroid, rxjava2, rxandroid2

logo

rb License

Download

Gradle:

implementation "com.blankj:rxbus:1.6"

How to use

非粘性事件

  1. 注册事件
public class YourActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 注册 String 类型事件
        RxBus.getDefault().subscribe(this, new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                Log.e("eventTag", s);
            }
        });

        // 注册带 tag 为 "my tag" 的 String 类型事件
        RxBus.getDefault().subscribe(this, "my tag", new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                Log.e("eventTag", s);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注销
        RxBus.getDefault().unregister(this);
    }
}
  1. 发送事件
// 发送 String 类型事件
RxBus.getDefault().post("without tag");

// 发送带 tag 为 "my tag" 的 String 类型事件
RxBus.getDefault().post("with tag", "my tag");

粘性事件(也就是先发送事件,在之后注册的时候便会收到之前发送的事件)

  1. 发送事件
// 发送 String 类型的粘性事件
RxBus.getDefault().postSticky("without tag");

// 发送带 tag 为 "my tag" 的 String 类型的粘性事件
RxBus.getDefault().postSticky("with tag", "my tag");

// 在需要移除粘性事件的地方移除它
RxBus.getDefault().removeSticky("without tag");
RxBus.getDefault().removeSticky("with tag", "my tag");
  1. 注册事件
public class YourActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 注册 String 类型事件
        RxBus.getDefault().subscribeSticky(this, new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                Log.e("eventTag", s);
            }
        });

        // 注册带 tag 为 "my tag" 的 String 类型事件
        RxBus.getDefault().subscribeSticky(this, "my tag", new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                Log.e("eventTag", s);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注销
        RxBus.getDefault().unregister(this);
    }
}

Nice wrap

如果用到事件总线的地方比较多,那么可以把事件总线的使用放到一个 Manager 中使用,比如我 Demo 中做的封装如下所示:

public class RxBusManager {

    private static final String MY_TAG = "MY_TAG";

    public static void subscribeRxBusManagerActivity(final RxBusManagerActivity activity){
        RxBus.getDefault().subscribe(activity, new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                activity.updateText("without " + s);
            }
        });

        RxBus.getDefault().subscribe(activity, MY_TAG, new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                activity.updateText("with " + s);
            }
        });
    }

    public static void postToRxBusManagerActivity(final String event) {
        RxBus.getDefault().post(event);
    }

    public static void postWithMyTagToRxBusManagerActivity(final String event) {
        RxBus.getDefault().post(event, MY_TAG);
    }

    public static void postStickyToRxBusManagerActivity(final String event) {
        RxBus.getDefault().postSticky(event);
    }

    public static void postStickyWithMyTagToRxBusManagerActivity(final String event) {
        RxBus.getDefault().postSticky(event, MY_TAG);
    }

    public static void unregisterRxBusManagerActivity(final RxBusManagerActivity activity) {
        RxBus.getDefault().unregister(activity);
    }
}

可以看出这是在 RxBusManagerActivity 中要使用 RxBus 的相关代码,这样可以更方便地管理应用中所有的事件总线,而不至于发了个事件都不清楚接收方在哪的尴尬。

How it comes

网上现有 RxBus 存有的问题:

  1. 使用的 RxBus 大多停留在 RxJava1 版本
  2. RxBus 实现的粘性事件很多都是有问题的
  3. 如果事件抛了异常,之后便再也无法接收到的问题
  4. 同类型事件需自己再次封装 Bean 进行区别。

介于以上问题,我还是亲自封装一个简洁的供大家使用,库已经依赖了 RxAndroid 和 RxJava,所以导入了该库的就不需要再额外导入那两库了。

当然,如果通信频率比较高追求效率的话还是推荐使用 EventBus

Principle

  1. 利用 FlowableProcessor 既可以作为观察者又可以作为被观察者来实现事件总线
  2. 粘性事件原理就是发送的时候把事件存到一个 hash 表中,在注册的时候查询下 hash 表中是否存在符合的事件,有的话就消费掉即可
  3. 替换原有 LambdaSubscriber 来让抛了异常之后可以继续接收到后续事件
  4. 封装了 TagMessage 来区分不同类别的 tag
  5. 动态识别范型对象来省去传入 Type 类型

还有一些细节就自己看源码去了解吧,总共有用的代码不超过 300 行哈。

Change log

打个小广告

欢迎加入我的小专栏「基你太美」一起学习。

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].