EventBus之EventBusBuilder

Configuration

EventBusBuilder类配置EventBus的各个方面。例如,下面是如何构建一个EventBus,在发布的事件没有订阅者时保持安静:

EventBus eventBus = EventBus.builder()
    .logNoSubscriberMessages(false)
    .sendNoSubscriberEvent(false)
    .build();

另一个例子是订阅者抛出异常时失败:

EventBus eventBus = EventBus.builder().throwSubscriberException(true).build();

注意:默认情况下,EventBus捕获(catch)从订阅者方法抛出的异常,并发送一个SubscriberExceptionEvent,该事件可能被处理,但不一定必须被处理。

Configure the default EventBus instance

Configure the default EventBus instance

使用EventBus.getDefault()是一种从应用程序的任何位置获取共享EventBus实例的简单方法。EventBusBuilder还允许使用installDefaultEventBus()方法配置这个默认实例。

例如,可以配置默认EventBus实例来重新抛出异常,这些异常发生在订阅方方法中。但我们只在调试构建时才这样做,因为这可能会在异常情况下导致应用程序崩溃:

EventBus.builder().throwSubscriberException(BuildConfig.DEBUG).installDefaultEventBus();

注意:在第一次使用默认EventBus实例之前,只能执行一次。对installDefaultEventBus()的后续调用将引发异常。这确保了应用程序中的行为一致。应用程序类是在使用默认EventBus实例之前配置它的好地方。

EventBusBuilder:

EventBusBuilderaddIndex(SubscriberInfoIndex index)

Adds an index generated by EventBus' annotation preprocessor.

添加由EventBus的注释预处理器生成的索引。

EventBusbuild()

Builds an EventBus based on the current configuration.

EventBusBuildereventInheritance(boolean eventInheritance)

By default, EventBus considers the event class hierarchy (subscribers to super classes will be notified).

默认情况下,EventBus考虑事件类层次结构(将通知超类的订阅者)。

EventBusBuilderexecutorService(java.util.concurrent.ExecutorService executorService)

Provide a custom thread pool to EventBus used for async and background event delivery.

为EventBus提供一个自定义线程池,用于异步和后台事件交付。

EventBusBuilderignoreGeneratedIndex(boolean ignoreGeneratedIndex)

Forces the use of reflection even if there's a generated index (default: false).

强制使用反射,即使生成了索引(默认值:false)。

EventBusinstallDefaultEventBus()

Installs the default EventBus returned by EventBus.getDefault() using this builders' values.

使用此构造器的值安装 EventBus. getdefault() 返回的默认EventBus。

必须在第一次使用默认EventBus之前只执行一次。

抛出:EventBusException——如果已经有一个默认的EventBus实例

EventBusBuilderlogNoSubscriberMessages(boolean logNoSubscriberMessages)

Default: true

EventBusBuilderlogSubscriberExceptions(boolean logSubscriberExceptions)

Default: true

EventBusBuildersendNoSubscriberEvent(boolean sendNoSubscriberEvent)

Default: true

EventBusBuildersendSubscriberExceptionEvent(boolean sendSubscriberExceptionEvent)

Default: true

EventBusBuilderskipMethodVerificationFor(java.lang.Class<?> clazz)

Method name verification is done for methods starting with onEvent to avoid typos; using this method you can exclude subscriber classes from this check.

对以onEvent开头的方法进行方法名验证,避免输入错误;使用此方法,您可以将订阅程序类排除在此检查之外。

还禁用方法修饰符的检查(公共的,不是静态的,也不是抽象的)。

EventBusBuilderstrictMethodVerification(boolean strictMethodVerification)

Enables strict method verification (default: false).

启用严格的方法验证(默认值:false)。

EventBusBuilderthrowSubscriberException(boolean throwSubscriberException)

Fails if an subscriber throws an exception (default: false).

如果订阅者抛出异常(默认:false),则失败。

提示:将其与BuildConfig一起使用。调试使应用程序在调试模式下崩溃(仅限)。这样,您就不会错过开发过程中的异常。

GitHub地址

由于作者水平有限,语言描述及代码实现中难免有纰漏,望各位看官多提宝贵意见!

Hello , World !

感谢所有!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

windfallsheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值