• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

注释处理生成代码的RxBus[Deprecated!]

武飞扬头像
枫叶思念
帮助1

Android RxBus

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

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

引用

gradle中加入:

  1.  
    dependencies {
  2.  
    compile 'com.github.vitess:rxbus:2.0.2'
  3.  
    annotationProcessor 'com.github.vitess:rxbus-compiler:2.0.2'
  4.  
    }
  • 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:

  1.  
    public class MainActivity extends AppCompatActivity {
  2.  
     
  3.  
    @Override
  4.  
    protected void onCreate(Bundle savedInstanceState) {
  5.  
    super.onCreate(savedInstanceState);
  6.  
    setContentView(R.layout.activity_main);
  7.  
    RxBus.register(this);
  8.  
    //TODO something
  9.  
    ...
  10.  
     
  11.  
    findViewById(R.id.button).setOnClickListenernew(View.OnClickListener() {
  12.  
    @Override
  13.  
    public void onClick(View v) {
  14.  
    RxBus.post("receiver1", 123);//post to receiver1
  15.  
    RxBus.post("This is post to receiver2");//post to receiver2
  16.  
    RxBus.post(new Object());//post to receiver3
  17.  
    RxBus.post("receiver4", null);//post to receiver4
  18.  
    RxBus.post(null);//post to receiver5
  19.  
    }
  20.  
    });
  21.  
    }
  22.  
     
  23.  
    @Override
  24.  
    protected void onDestroy() {
  25.  
    super.onDestroy();
  26.  
    RxBus.unregister(this);
  27.  
    }
  28.  
     
  29.  
    @Subscribe("receiver1")
  30.  
    @RxThread(ThreadType.IO)
  31.  
    public void receiver1(int random) {
  32.  
    Log.i("RxBus", "receiver1:" Thread.currentThread().getName());
  33.  
    }
  34.  
     
  35.  
    @Subscribe
  36.  
    @RxThread(ThreadType.Single)
  37.  
    public void receiver2(String str) {
  38.  
    Log.i("RxBus", "receiver2:" Thread.currentThread().getName());
  39.  
    }
  40.  
     
  41.  
    @Subscribe
  42.  
    public void receiver3(Object obj) {
  43.  
    Log.i("RxBus", "receiver3:" Thread.currentThread().getName());
  44.  
    }
  45.  
     
  46.  
    @Subscribe("receiver4")
  47.  
    public void receiver4(){
  48.  
    Log.i("RxBus", "receiver4:" Thread.currentThread().getName());
  49.  
    }
  50.  
     
  51.  
    @Subscribe
  52.  
    public void receiver5(){
  53.  
    Log.i("RxBus", "receiver5:" Thread.currentThread().getName());
  54.  
    }
  55.  
    }
  • 1

限制

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

TODO

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

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

License

  1.  
    Copyright 2017 Vincent Tam
  2.  
     
  3.  
    Licensed under the Apache License, Version 2.0 (the "License");
  4.  
    you may not use this file except in compliance with the License.
  5.  
    You may obtain a copy of the License at
  6.  
     
  7.  
    http://www.apache.org/licenses/LICENSE-2.0
  8.  
     
  9.  
    Unless required by applicable law or agreed to in writing, software
  10.  
    distributed under the License is distributed on an "AS IS" BASIS,
  11.  
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12.  
    See the License for the specific language governing permissions and
  13.  
    limitations under the License.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfikkje
系列文章
更多 icon
同类精品
更多 icon
继续加载