【发布时间】:2018-12-05 17:57:48
【问题描述】:
我要做的是制作一个远程配置提取的单例,以便在我的 SplashScreen 中首次加载它。
在加载启动画面时,我使用远程配置单例获取数据一次,然后,我只需访问每个类中的值,我需要该远程配置的值
我这样做是为了防止用户在 Activity 上看到更改,这是由于我正在制作圣诞主题以显示带有远程配置的自定义布局
一切正常(有时),主题在整个应用程序中都在使用这个单例进行更改,但有时在我的 SplashScreen 加载后必须返回的异步获取时间,所以我可以看到主题发生了变化我的其他活动,但不是在 SplashScreen 之后的主要活动
这是我的单身
public class PruebaSingleton {
private boolean christmasEvent;
private FirebaseRemoteConfig mFirebaseRemoteConfig;
private static volatile PruebaSingleton _instance;
private PruebaSingleton(){
fetchRemoteConfig();
}
public synchronized static PruebaSingleton getInstance(){
if(_instance == null){
synchronized (PruebaSingleton.class) {
if (_instance == null) _instance = new PruebaSingleton();
}
}
return _instance;
}
private void fetchRemoteConfig() {
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
mFirebaseRemoteConfig.setConfigSettings(
new FirebaseRemoteConfigSettings.Builder().setDeveloperModeEnabled(BuildConfig.DEBUG)
.build());
mFirebaseRemoteConfig.fetch(0).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
mFirebaseRemoteConfig.activateFetched();
}
christmasEvent = mFirebaseRemoteConfig.getBoolean("christmas_ottaa_mode");
}).addOnFailureListener(e -> {
});
}
public boolean isChristmasModeEnabled(){
return christmasEvent;
}
现在,在这之后,我只需实例化一次这个单例以获取远程配置的数据并影响我的整个应用程序。
在我的SplashScreen onCreate()
PruebaSingleton.getInstance();
然后我就以这种方式在我的所有活动中获取布尔值
PruebaSingleton.getInstance().isChristmasModeEnabled();
然后,我可以更改主题。
问题是,有时(不通常,但它在大约 10 次启动中发生 2 次)在启动屏幕发送到第一个 Activity 后,从我的 SplashScreen 中的单例获取数据,这导致我的第一个 Activity 不显示主题,但我的其他活动显示。
我的问题是
当我在 SplashScreen 中时,是否有办法处理提取?
考虑制作一个界面会稍微减慢我的 SplashScreen,直到所有的提取工作都在单例类中完成,但我也不想在第一个 Activity 上显示任何弹出对话框来告诉用户正在等待提取.
这更多是一个性能问题,因为在完成获取时,启动画面需要快速进入第一个活动,而不是等待更多时间来获取数据,因为如果发生这种情况,我会降低应用程序的性能尝试加载圣诞主题。
第一次获取也应该正常工作,第二次启动将加载第一次获取的数据,然后等待 12 小时来请求新数据,所以我需要让所有 fetch 值为 true 的活动第一次。
【问题讨论】:
标签: java android firebase firebase-remote-config