FirebaseRemoteConfig.fetch()不会每次都触发OnCompleteListener [英] FirebaseRemoteConfig.fetch() does not trigger OnCompleteListener every time

查看:1156
本文介绍了FirebaseRemoteConfig.fetch()不会每次都触发OnCompleteListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实施Firebase Remote Config:

 重写onCreate(savedInstanceState:Bundle?){

configSettings = FirebaseRemoteConfigSettings.Builder()。setDeveloperModeEnabled(BuildConfig.DEBUG).build()
$ b mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance()
mFirebaseRemoteConfig.setConfigSettings(configSettings)
mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults)
fetchRemoteConfig()
}

private fun fetchRemoteConfig(){
var cacheExpiration = 3600L $ b $如果(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)
// ...
}

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


OnCompleteListener应该总是被调用吗?即使我点击缓存?
$ b编辑:即使我禁用developper模式的行为是相同的。有时回调被触发,有时不会。

解决方案

我正面临同样的问题并联系了Firebase支持。他们回答如下:


目前有一个bug报告,onComplete,onSuccess和onFailure侦听器没有被调用if fetch()被称为太早。 [...]
目前有一个工作,你可以把fetch()放在postResume中。

我实施了相应的解决方法

  protected void onPostResume(){
super.onPostResume();
$ b $ mFirebaseRemoteConfig.fetch(cacheExpiration)
.addOnSuccessListener(new OnSuccessListener< Void>(){
@Override $ b $ public void onSuccess(void aVoid){
Log.d(TAG,Fetch Succeeded);
//一旦配置成功获取,它必须在返回新获取的值之前被激活
mFirebaseRemoteConfig.activateFetched();
//做任何事情都应该成功


.addOnFailureListener(new OnFailureListener(){
@Override
public void onFailure(@NonNull异常异常){
Log.d(TAG,Fetch failed);
//在失败时做任何事情
}
});



$ b $ p
$ b

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



更新:

我刚刚从firebase支持中获得通知。根据他们的问题,解决了最新的Google Play服务更新。


修复远程配置在获取后不会调用监听器在最新的Google Play服务更新中。
我现在要结束这个案子。但是,如果您仍然遇到问题,请随时与我联系并通知我。



I'm trying to implement Firebase Remote Config :

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")
    //...
}

My problem is that the OnCompleteListener is not always called. If I close/open my app several times, the setupView() is not always triggered.

The OnCompleteListener should always be called right? Even if I'm hitting cache?

EDIT: Even if I disable the developper mode the behavior is the same. Sometimes the callback is triggered, sometimes not.

解决方案

I was facing the same issue and contacted the firebase support. They replied the following:

There currently is a bug that has been reported where onComplete, onSuccess, and onFailure listeners doesn't get called if fetch() is called too early. [...] Currently there is a work around where you can put the fetch() inside a postResume. You can try using this in the meantime before a solution has been released.

I implemented the workaround accordingly

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
                }
            });
}

So far it seems their proposed workaround has resolved the issue.

UPDATE:

I just got notice from the firebase support. According to them the issue is resolved with the latest Google Play Services update.

A fix to Remote Config not calling listeners after fetching has been released in the newest Google play services update. I'll be closing this case for now. However if you are still experiencing issues, feel free to reach out and let me know.

这篇关于FirebaseRemoteConfig.fetch()不会每次都触发OnCompleteListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆