基于注释处理生成代码的RxBus[Deprecated!]
创始人
2024-02-09 01:06:39
0

Android RxBus

该项目基于RxJava2 & RxAndroid,并且从AndroidKnife/RxBus中学习而实现的。

使用annotation processing(注释处理)自动生成模板代码,避免了反射带来的性能影响。通过@Subscribe标记订阅方法,@Rxthread可设置订阅方法的运行线程,线程支持RxJava中提供的6种线程MainThreadIOComputationSingleNewThreadTrampoline

引用

在gradle中加入:

dependencies {compile 'com.github.vitess:rxbus:2.0.2'annotationProcessor 'com.github.vitess:rxbus-compiler:2.0.2'
}
  • 1

开发版本的快照可从Sonatype’s snapshots repository中找到。

使用

在类的初始化处使用RxBus.register注册,并在类销毁的地方使用RxBus.unregister注销。注册后的类中的方法即可使用@Subscribe注释标记,此后在类以外的地方即可通过RxBus.post发射数据到指定方法中。

当使用@Subscribe标记方法时,若不指定特定的tag,该方法将被默认的tag所标记。这一类被默认tag标记的方法可接收RxBus.post(Object value)发射数据,或者使用RxBus.post(Subscribe.DEFAULT , ${value})来显式发射。

For example:

public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);RxBus.register(this);//TODO something...findViewById(R.id.button).setOnClickListenernew(View.OnClickListener() {@Overridepublic void onClick(View v) {RxBus.post("receiver1", 123);//post to receiver1RxBus.post("This is post to receiver2");//post to receiver2RxBus.post(new Object());//post to receiver3RxBus.post("receiver4", null);//post to receiver4RxBus.post(null);//post to receiver5}});}@Overrideprotected void onDestroy() {super.onDestroy();RxBus.unregister(this);}@Subscribe("receiver1")@RxThread(ThreadType.IO)public void receiver1(int random) {Log.i("RxBus", "receiver1:" + Thread.currentThread().getName());}@Subscribe@RxThread(ThreadType.Single)public void receiver2(String str) {Log.i("RxBus", "receiver2:" + Thread.currentThread().getName());}@Subscribepublic void receiver3(Object obj) {Log.i("RxBus", "receiver3:" + Thread.currentThread().getName());}@Subscribe("receiver4")public void receiver4(){Log.i("RxBus", "receiver4:" + Thread.currentThread().getName());}@Subscribepublic void receiver5(){Log.i("RxBus", "receiver5:" + Thread.currentThread().getName());}
}
  • 1

限制

  1. 目前支持发送null值(虽然post方法标记了@NonNull)
  2. 不支持发送实现了Map、Collection接口的参数类型(如ArrayList、HashMap等),如果必须发送这种集合容器参数,请自实现实体类,集合容器作为成员变量,然后发送实体类参数

TODO

目前思路稍微有些瓶颈,如果有好点子或者有可改进的地方,欢迎pull request,thanks!

  • 增加单元测试
  • 优化Processor性能
  • 优化模板代码
  • 优化Processor的缓存方式和生成模式
  • 增加sticky事件支持
  • 根据使用方式分别生成不同的Observable,使用频率较少的用post方法发射,每次独立生成Single完成操作;使用频率较高且生命周期较长的使用continuePost方法发射,仅生成Processor完成操作
  • etc.

License

Copyright 2017 Vincent TamLicensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...