Android Firebase远程配置初始获取未返回值 [英] Android Firebase Remote Config initial fetch does not return value

查看:125
本文介绍了Android Firebase远程配置初始获取未返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首次使用应用程序首次打开时,我正在使用Firebase Remote Config来获取数据.但是问题是我无法在应用程序的首次启动时获取数据. onComplete()方法触发但不返回任何值.如果我关闭应用程序并再次运行,它将从远程配置中返回值.

I'm using Firebase Remote Config to fetch data when app first open for the first time. But problem is I cannot fetch data on first start of the app. onComplete() method triggers but returns no value. If I close app and run again it returns value from Remote Config.

试图在onCreate()onStart()onResume()中调用fetch(),给了第二个延迟,而postDelay()仍然相同,初始提取始终为空.我知道,Remote Config有setDefaults()方法可以在获取默认值之前存储默认值,但是我不需要在应用程序内部设置默认值.

Tried to call fetch() in onCreate(), onStart(), onResume(), gave it a second delay with postDelay() still the same, initial fetch is always empty. I know, Remote Config has setDefaults() method to store defaults before it is getting fetched, but setting defaults inside app is not what I want.

这是远程配置的工作方式,还是我做错了什么?我发现的唯一解决方法是在onResume()内添加fetch()并在onCreate()内再次调用onResume().结果两次调用onResume().第一次是Android系统,第二次是代码.

It is the way how Remote Config works or am I doing something wrong? The only workaround I found is to add fetch() inside onResume() and call onResume() again inside onCreate(). It results calling onResume() twice. First time by Android system and second time by code.

还有其他方法可以强制Remote Config在首次运行时获取数据吗?

It there any other way to force Remote Config fetch data on first time run?

更新

首先在onComplete()内部,必须先调用firebaseRemoteConfig.activateFetched();,然后再从中获取新值.

Inside onComplete() first I must call firebaseRemoteConfig.activateFetched(); before getting new values from it.

推荐答案

mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
        FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder().build();
        mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings)
                .addOnCompleteListener(task -> mFirebaseRemoteConfig.fetchAndActivate()
                        .addOnCompleteListener(activity, task1 {
                       // now cache is updated you can fetch values
                       String value = mFirebaseRemoteConfig.getString("key");
               }));

在完成配置设置后开始获取值

Start getting the values after config settings are completed

只需在应用启动时执行此操作,然后直接在其他任何地方获取值即可(例如: mFirebaseRemoteConfig.getString("key");)

just do that at the app launch, and get values directly anywhere else (eg: mFirebaseRemoteConfig.getString("key");)

这篇关于Android Firebase远程配置初始获取未返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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