Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Mar 29, 2022. It is now read-only.
/ApolloPublic archive

🚀 Awesome EventBus by RxJava.

License

NotificationsYou must be signed in to change notification settings

Sloaix/Apollo

Repository files navigation

EventBus by RxJava

  • 基于RxJava
  • 基于编译时注解技术,编译时生成绑定代码,非反射,性能0损耗
  • 支持进程间通信
  • 支持@Tag注解,支持多重Tag
  • 支持@Sticky注解
  • 支持@Take注解,接收指定次数事件
  • 支持@SubscribeOn注解,指定订阅线程
  • 支持@ObserveOn注解,指定观察线程
  • 支持@Backpressure注解,支持3种背压策略(BackpressureStrategy.BUFFER,BackpressureStrategy.DROP,BackpressureStrategy.LATEST)
  • 支持apollo core 非注解式使用

English Document

Todo

  • 重写demo
  • 完整的单元测试(已完成30%,见unittest module)

开始

用3分钟时间快速集成Apollo

集成

使用jitpack第三方依赖库

allProjects {  repositories {    maven { url"https://www.jitpack.io" }  }}

在项目所在build.gralde添加依赖

dependencies {//Apollo依赖RxAndroid2,请使用最新的版本  implementation"io.reactivex.rxjava2:rxandroid:2.0.1"//Apollo的核心库  implementation"com.github.lsxiao.Apollo:core:1.0.2"//IPC,如不需要可以不依赖  implementation"com.github.lsxiao.Apollo:ipc:1.0.2"//Apollo的编译时注解处理器  annotationProcessor"com.github.lsxiao.Apollo:processor:1.0.2"//如果你使用的是kotlin,请使用kapt  kapt"com.github.lsxiao.Apollo:processor:1.0.2"}

使用

初始化

ApolloBinderGeneratorImpl在编译时生成。

Apollo.init(AndroidSchedulers.mainThread(),this);

绑定/解绑

为了避免内存泄露,应在组件生命周期内绑定和解绑。

publicabstractclassBaseActivityextendsActivity {privateApolloBindermBinder;@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {        ...mBinder =Apollo.bind(this);    }@OverrideprotectedvoidonDestroy() {        ...if(mBinder !=null){mBinder.unbind();                }    }    ...}

发送

让发送更简单

Apollo.emit("event","hello apollo")

接收

让接收更自在

@Receive("event")publicvoidonEvent(Stringmessage){    ...}

进程间通信(IPC)

默认关闭

Apollo.init(AndroidSchedulers.mainThread(), this,true);

**!!!注意:**由于默认采用kryo序列化,所以任何需要在进程间传输的数据对象,自己包括其内部的成员对象都必须有一个默认的无参构造函数!!!

高级用法

注解

注意!!!,被注解的函数一定得是public修饰,且@Receive是必须注解,其余为可选注解。

注解参数描述默认值
@Receive接收一个字符串tag数组,或者单个字符串tag
@Stickyremove接收后是否清除stikcy事件ture
@SubscribeOn订阅所在线程SchedulerProvider.Tag.IO
@ObserveOn观察所在线程SchedulerProvider.Tag.MAIN
@Take接收多少次事件,int型参数
@Backpressure背压策略(BackpressureStrategy.BUFFER,BackpressureStrategy.DROP,BackpressureStrategy.LATEST)

更多方法

booleansticky =true;//只有tagApollo.emit("tag");//tag和数据实体Apollo.emit("tag","event");//stikcy(只有被@Sticky注解的函数才能收到sticky事件)Apollo.emit("tag","event",stikcy)//只有tag的stikcy调用Apollo.emit("tag",sticky)

注意,在emit一个boolean变量的时候,正确写法:

Apollo.emit("tag",true,sticky)

错误写法,这样直接就发送了一个不带参数的sticky事件:

Apollo.emit("tag",true)

自定义Serializer

Apollo默认采用kryo来序列化IPC数据对象,你可以提供一个Serializer来修改默认的实现。

Apollo.serializer(newSerializable() {@NotNull@Overridepublicbyte[]serialize(@NotNullObjectobj) {        ...    }@Overridepublic <T>Tdeserialize(@NotNullbyte[]data,@NotNullClass<T>clazz) {        ...    }});

混淆

-dontwarn com.esotericsoftware.kryo.**-dontwarn org.objenesis.instantiator.**-dontwarn org.codehaus.**-dontwarn java.nio.**-dontwarn java.lang.invoke.**-keep class com.lsxiao.apollo.generate.** { *; }

测试

全面而完善的测试是开源库质量的保证,目前测试用例正在不断完善中:)

core module

KryoSerializeTest

unit test module

unittest

构建于ReactiveX之上

  • RxJava2 - Reactive Extensions for the JVM
  • RxAndroid2 - Reactive Extensions for Android

版本

我们使用语义化版本控制规范 作为版本管理,有关可用的版本,请参阅此标签列表

作者

更多贡献者 请参考这个项目的列表。

开源许可

Apache License Version 2.0


[8]ページ先頭

©2009-2025 Movatter.jp