如何解决使用 RxJava 的 Firebase RemoteConfig
我正在尝试将 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 变量,以便稍后使用。所述字符串变量不能为 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()
})
解决方法
设法让它工作似乎我只需要使用 tryOnError
因为当发射器被处置/取消或不再可用时,它会自动处理这种情况。使用 Completable
而不是 Observable
也可以。
fun init(context: Context): Observable<Boolean> {
remoteConfig.setConfigSettingsAsync(remoteConfigSettings)
return Observable.create { emitter ->
remoteConfig.fetchAndActivate().addOnSuccessListener {
//Use the latest configuration
assignSource(context)
Log.wtf("CONFIG","SUCCESS")
emitter.onNext(it)
}.addOnFailureListener {
//Try to use old configuration instead
assignSource(context)
Log.wtf("CONFIG","FAILED")
emitter.tryOnError(it.cause!!) // Try to throw an error if emitter still available or if the sequence is not cancelled/disposed
FirebaseCrashlytics.getInstance().recordException(it)
}.addOnCompleteListener {
emitter.onComplete()
Log.wtf("CONFIG","COMPLETE")
}
}
}
飞溅
cryptonHadItsChance = RemoteConfig.init(this)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
proceedToMain()
},{
//Only show dialog if necessary field is not available and activity still running
if (Singleton.staticVariable.isEmpty() && !isFinishing && !isDestroyed)
alertDialog.show()
else
proceedToMain()
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。