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:
EventBusBuilder | addIndex(SubscriberInfoIndex index) Adds an index generated by EventBus' annotation preprocessor. 添加由EventBus的注释预处理器生成的索引。 |
EventBus | build() Builds an EventBus based on the current configuration. |
EventBusBuilder | eventInheritance(boolean eventInheritance) By default, EventBus considers the event class hierarchy (subscribers to super classes will be notified). 默认情况下,EventBus考虑事件类层次结构(将通知超类的订阅者)。 |
EventBusBuilder | executorService(java.util.concurrent.ExecutorService executorService) Provide a custom thread pool to EventBus used for async and background event delivery. 为EventBus提供一个自定义线程池,用于异步和后台事件交付。 |
EventBusBuilder | ignoreGeneratedIndex(boolean ignoreGeneratedIndex) Forces the use of reflection even if there's a generated index (default: false). 强制使用反射,即使生成了索引(默认值:false)。 |
EventBus | installDefaultEventBus() Installs the default EventBus returned by EventBus.getDefault() using this builders' values. 使用此构造器的值安装 EventBus. getdefault() 返回的默认EventBus。 必须在第一次使用默认EventBus之前只执行一次。 抛出:EventBusException——如果已经有一个默认的EventBus实例 |
EventBusBuilder | logNoSubscriberMessages(boolean logNoSubscriberMessages) Default: true |
EventBusBuilder | logSubscriberExceptions(boolean logSubscriberExceptions) Default: true |
EventBusBuilder | sendNoSubscriberEvent(boolean sendNoSubscriberEvent) Default: true |
EventBusBuilder | sendSubscriberExceptionEvent(boolean sendSubscriberExceptionEvent) Default: true |
EventBusBuilder | skipMethodVerificationFor(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开头的方法进行方法名验证,避免输入错误;使用此方法,您可以将订阅程序类排除在此检查之外。 还禁用方法修饰符的检查(公共的,不是静态的,也不是抽象的)。 |
EventBusBuilder | strictMethodVerification(boolean strictMethodVerification) Enables strict method verification (default: false). 启用严格的方法验证(默认值:false)。 |
EventBusBuilder | throwSubscriberException(boolean throwSubscriberException) Fails if an subscriber throws an exception (default: false). 如果订阅者抛出异常(默认:false),则失败。 提示:将其与BuildConfig一起使用。调试使应用程序在调试模式下崩溃(仅限)。这样,您就不会错过开发过程中的异常。 |
由于作者水平有限,语言描述及代码实现中难免有纰漏,望各位看官多提宝贵意见!
Hello , World !
感谢所有!