基于注释处理生成代码的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.

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...