【发布时间】:2021-07-05 23:28:15
【问题描述】:
我正在尝试将 RxJava 与 FirebaseRemoteConfig 一起使用,但不确定如何使这两个工作,尝试使用 Completable 但我收到错误 The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with.
我有这个问题,我需要从 RemoteConfig 中获取,然后使用这个最新的配置从 MySingleton.class 初始化 String 变量以供以后使用。所述 String 变量不能为 null 或为空,因此流是。
飞溅时
- 致电
fetchAndActivate - 同时监听 OnSuccess 和 OnFailure
- 如果成功使用最新配置初始化静态字符串变量
- 如果失败,请尝试使用旧的/缓存的配置
- 缓存可能在首次运行时尚不存在,因此请检查静态字符串变量是否为空
- 如果为空则显示
AlertDialog进行重试。 - 如果不继续进行主要活动。
我想做的是使用 RxJava 并侦听 OnSuccess 或 OnFailure 侦听器,将来在使用 Firebase Firestore 时仅获取单个文档时,我可能也可以应用这些侦听器。
我该怎么做?
到目前为止,这是我得到的
class RemoteConfig {
companion object {
private val remoteConfig: FirebaseRemoteConfig by lazy {
FirebaseRemoteConfig.getInstance()
}
private val remoteConfigSettings: FirebaseRemoteConfigSettings by lazy {
FirebaseRemoteConfigSettings.Builder()
.setMinimumFetchIntervalInSeconds(1800)
.build()
}
fun init(context: Context): Completable {
remoteConfig.setConfigSettingsAsync(remoteConfigSettings)
return Completable.create {
fetchConfig(context)
}
}
private fun fetchConfig(context: Context): Completable {
return Completable.create { emitter ->
remoteConfig.fetchAndActivate().addOnSuccessListener {
//Use the latest configuration
assignSource(context)
emitter.onComplete()
}.addOnFailureListener {
//Try to use old configuration instead
assignSource(context)
emitter.onError(it.cause!!)
FirebaseCrashlytics.getInstance().recordException(it)
}
}
}
private fun assignSource(context: Context) {
Singleton.staticVariable=
remoteConfig.getString(context.getString(R.string.key))
}
}
}
飞溅活动
Completable.mergeArray(
RemoteConfig.init(this).subscribeOn(Schedulers.io()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.timeout(5, TimeUnit.SECONDS)
.subscribe({
proceedToMain()
}, {
if (Singleton.staticVariable.isEmpty())
AlertDialog.Builder(this)
.setMessage(it.message)
.setPositiveButton("Retry"
) { dialog, _ ->
run {
dialog.dismiss()
fetchConfig()
}
}
.setNegativeButton("Exit"
) { dialog, _ ->
dialog.dismiss()
finish()
}
.setCancelable(false)
.show()
else
proceedToMain()
})
【问题讨论】:
-
应用在前台时需要每30分钟抓取一次吗?
-
否,将更新问题。无论如何,我也设法让它工作。
标签: android firebase kotlin rx-java2 firebase-remote-config