FirebaseRemoteConfig.fetch()不会每次都触发OnCompleteListener

gui*_*ume 28 android firebase firebase-remote-config

我正在尝试实施Firebase远程配置:

override fun onCreate(savedInstanceState: Bundle?) {

    val configSettings = FirebaseRemoteConfigSettings.Builder().setDeveloperModeEnabled(BuildConfig.DEBUG).build()

    mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance()
    mFirebaseRemoteConfig.setConfigSettings(configSettings)
    mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults)
    fetchRemoteConfig()
}

private fun fetchRemoteConfig() {
    var cacheExpiration = 3600L
    if (mFirebaseRemoteConfig.info.configSettings.isDeveloperModeEnabled) {
        cacheExpiration = 0L
    }

    mFirebaseRemoteConfig.fetch(cacheExpiration)
        .addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    Log.d(TAG, "Remote config fetch succeeded")
                    mFirebaseRemoteConfig.activateFetched()
                } else {
                    Log.d(TAG, "Remote config fetch failed - ${task.exception?.message}")
                }

                setupView()
            }
}

private fun setupView() {
    val text = mFirebaseRemoteConfig.getString("my_text")
    //...
}
Run Code Online (Sandbox Code Playgroud)

我的问题是并不总是调用OnCompleteListener.如果我多次关闭/打开我的应用程序,则不会始终触发setupView().

应该始终调用OnCompleteListener吗?即使我正在打缓存?

编辑:即使我禁用开发者模式,行为也是一样的.有时会触发回调,有时不会.

Max*_*Max 27

我遇到了同样的问题并联系了firebase支持.他们回复了以下内容:

目前有一个错误报告,如果过早调用fetch(),则不会调用onComplete,onSuccess和onFailure侦听器.[...]目前有一个工作可以将fetch()放在postResume中.在解决方案发布之前,您可以尝试同时使用它.

我相应地实施了变通方法

protected void onPostResume() {
    super.onPostResume();

    mFirebaseRemoteConfig.fetch(cacheExpiration)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.d(TAG, "Fetch Succeeded");
                    // Once the config is successfully fetched it must be activated before newly fetched values are returned.
                    mFirebaseRemoteConfig.activateFetched();
                    // Do whatever should be done on success
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    Log.d(TAG, "Fetch failed");
                    // Do whatever should be done on failure
                }
            });
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,似乎他们提出的解决方案已经解决了这个问题

更新:

我刚收到firebase支持的通知.根据他们的说法,最新的Google Play服务更新解决了这个问题.

在最新的Google Play服务更新中发布了在获取后不调用侦听器的远程配置修复程序.我现在要结案.但是,如果您仍然遇到问题,请随时与我联系并告知我们.

  • 不幸的是,在postResume上调用fetch()对我来说似乎也不可靠. (5认同)
  • 谢谢,马克斯!他们是否提供了跟踪此错误的链接? (2认同)

dan*_*anb -1

Firebase 的更新版本 9.2.0 可以正常工作,并且不再需要此 hack。

我的这个“工作”可靠......但你可能不喜欢我的解决方案。为了在 Firebase 准备就绪时进行配置获取,我必须这样做:

FirebaseAuth.getInstance()
   // I don't actually want to or need to sign in..(and this actually throws an error for us.. but we ignore it)
  .signInAnonymously()
  // when it completes (error or no error) we can do our business
  .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
      @Override
      public void onComplete(@NonNull Task<AuthResult> task) {
        // do the remote config fetch you were doing before
        remoteConfig.fetch(...).addOnComplete(...);
      }
  });
Run Code Online (Sandbox Code Playgroud)

这确保了 firebase 内部准备好执行初始配置获取...在第一个应用程序打开时,这在我蹩脚的测试设备上似乎需要大约 6-10 秒(整个过程包括身份验证和配置获取)。随后打开整个过程大约需要 2-5 秒。显然,这都是任意的,具体取决于设备/网络和 YMMV。

我很想知道为什么需要这样做......似乎远程配置应该能够在内部管理它而不是将其暴露给我们。

ps 除了 firebase-config 之外,您还需要此依赖项

compile 'com.google.firebase:firebase-auth:9.0.1'