注释处理生成代码的RxBus[Deprecated!]
Android RxBus
该项目基于RxJava2 & RxAndroid,并且从AndroidKnife/RxBus中学习而实现的。
使用annotation processing(注释处理)自动生成模板代码,避免了反射带来的性能影响。通过@Subscribe
标记订阅方法,@Rxthread
可设置订阅方法的运行线程,线程支持RxJava
中提供的6种线程:MainThread
、IO
、Computation
、Single
、NewThread
、Trampoline
。
引用
在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 {
-
-
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
RxBus.register(this);
-
//TODO something
-
...
-
-
findViewById(R.id.button).setOnClickListenernew(View.OnClickListener() {
-
-
public void onClick(View v) {
-
RxBus.post("receiver1", 123);//post to receiver1
-
RxBus.post("This is post to receiver2");//post to receiver2
-
RxBus.post(new Object());//post to receiver3
-
RxBus.post("receiver4", null);//post to receiver4
-
RxBus.post(null);//post to receiver5
-
}
-
});
-
}
-
-
-
protected void onDestroy() {
-
super.onDestroy();
-
RxBus.unregister(this);
-
}
-
-
"receiver1")(
-
ThreadType.IO)(
-
public void receiver1(int random) {
-
Log.i("RxBus", "receiver1:" Thread.currentThread().getName());
-
}
-
-
-
ThreadType.Single)(
-
public void receiver2(String str) {
-
Log.i("RxBus", "receiver2:" Thread.currentThread().getName());
-
}
-
-
-
public void receiver3(Object obj) {
-
Log.i("RxBus", "receiver3:" Thread.currentThread().getName());
-
}
-
-
"receiver4")(
-
public void receiver4(){
-
Log.i("RxBus", "receiver4:" Thread.currentThread().getName());
-
}
-
-
-
public void receiver5(){
-
Log.i("RxBus", "receiver5:" Thread.currentThread().getName());
-
}
-
}
- 1
限制
- 目前支持发送null值(虽然post方法标记了@NonNull)
- 不支持发送实现了Map、Collection接口的参数类型(如ArrayList、HashMap等),如果必须发送这种集合容器参数,请自实现实体类,集合容器作为成员变量,然后发送实体类参数
TODO
目前思路稍微有些瓶颈,如果有好点子或者有可改进的地方,欢迎pull request,thanks!
- 增加单元测试
- 优化Processor性能
- 优化模板代码
- 优化Processor的缓存方式和生成模式
- 增加sticky事件支持
- 根据使用方式分别生成不同的Observable,使用频率较少的用
post
方法发射,每次独立生成Single
完成操作;使用频率较高且生命周期较长的使用continuePost
方法发射,仅生成Processor
完成操作 - etc.
License
-
Copyright 2017 Vincent Tam
-
-
Licensed 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 at
-
-
http://www.apache.org/licenses/LICENSE-2.0
-
-
Unless 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.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfikkje
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13