【问题标题】:Firebase RemoteConfig with RxJavaFirebase RemoteConfig 与 RxJava
【发布时间】: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 或为空,因此流是。

飞溅时

  1. 致电fetchAndActivate
  2. 同时监听 OnSuccess 和 OnFailure
  3. 如果成功使用最新配置初始化静态字符串变量
  4. 如果失败,请尝试使用旧的/缓存的配置
  5. 缓存可能在首次运行时尚不存在,因此请检查静态字符串变量是否为空
  6. 如果为空则显示AlertDialog 进行重试。
  7. 如果不继续进行主要活动。

我想做的是使用 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


【解决方案1】:

设法使其工作似乎我只需要使用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()
            })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多