远程配置取值不一致
Remote config value fetching is inconsistent
我正在 onStart() 和 onStop() 中获取 Firebase 远程配置值。我在 "min_version" 中持有的变量,用于应用内更新功能。所以我的问题是,当我要远程配置和更新值时,它们不会立即更新,而是在更新为新值之前保存它们的旧值以进行另一个应用程序生命周期迭代。这意味着,如果我决定要更新某个版本的所有用户,他们将不会立即获得应用内更新,而只会在通过 onStop 进行一次迭代后获得它。
这是我的 onStart() 和 onStop() 代码 -
@Override
protected void onStart() {
/**
* We are fetching the minimum version that we want the user to have from Firebase Remote Config, only after we have the results we can proceed to start the app.
*/
getMinAppVersion("onStart", () -> {
// navigation drawer
checkValidFacebookSession();
initDrawerMenu();
// network monitoring
registerNetworkReceiver();
// monitoring upload
LocalBroadcastManager.getInstance(this).registerReceiver(mUploadReceiver, new IntentFilter(ULBroadcastConstants.UPLOAD_STATUS_ACTION));
LocalBroadcastManager.getInstance(this).registerReceiver(mFCMReceiver, new IntentFilter(MyFirebaseMessagingService.RECEIVED_FCM_ACTION));
checkInAppUpdate();
});
super.onStart();
}
@Override
protected void onStop() {
getMinAppVersion("onStop", () -> {
mNavigationView.setNavigationItemSelectedListener(null);
mDrawerLayout.removeDrawerListener(mBadgeDrawerToggle);
// network monitor
unregisterNetworkReceiver();
// unregister upload
LocalBroadcastManager.getInstance(this).unregisterReceiver(mUploadReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mFCMReceiver);
});
super.onStop();
}
这是我的 'getMinAppVersion()' 方法 -
private void getMinAppVersion(String didComeFrom, OnRemoteConfigFetchComplete listener){
//fetching the min_version parameter from 'remote config' of Firebase and saves it to our local variable.
FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder().setMinimumFetchIntervalInSeconds(200).build();
mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
mFirebaseRemoteConfig.fetch(0);
mFirebaseRemoteConfig.activate().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
min_version = mFirebaseRemoteConfig.getLong(RemoteConfigUtil.MIN_VERSION);
Timber.tag("min_version_" + didComeFrom).d(String.valueOf(min_version));
if (listener != null)
listener.onFetchComplete();
} else {
Timber.tag("min version").d("error while fetching and activating remove config");
}
});
}
控制台中的远程配置更新不是即时的。它们不像推送通知那样工作。新值仅在 fetch() completes, and you call activate() 返回的 Task 之后生效。我要指出的是,您没有使用 fetch() 返回的任务来查明何时从 Firebase 收到了新值。
这就是它发生的原因。 Remote Config
根据官方文档将值缓存在本地存储中。您可以参考这里并考虑
"Remote Config includes a client library that handles important tasks like fetching parameter values and caching them, while still giving you control over when new values are activated so that they affect your app's user experience. This lets you safeguard your app experience by controlling the timing of any changes."
当您使用客户端库获取远程配置参数时,缓存的值将返回给您(如果有的话)(TL;DL) 更多信息,您可以阅读官方文档here。它使用最小时间间隔来获取值以避免应用程序崩溃,对于该最小间隔时间,最后获取的值用作缓存
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setMinimumFetchIntervalInSeconds(3600)
.build();
mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
这里3600
是minimum interval
的时间间隔,您可以根据自己的需要调整时间。如果您最近调用了 fetch()
并且再次调用它,客户端库会根据 minimum-interval
时间确定应该执行新的 API 调用或提供缓存值。 Firebase 远程配置适用于 REST api,也使用 Throttling。
我正在 onStart() 和 onStop() 中获取 Firebase 远程配置值。我在 "min_version" 中持有的变量,用于应用内更新功能。所以我的问题是,当我要远程配置和更新值时,它们不会立即更新,而是在更新为新值之前保存它们的旧值以进行另一个应用程序生命周期迭代。这意味着,如果我决定要更新某个版本的所有用户,他们将不会立即获得应用内更新,而只会在通过 onStop 进行一次迭代后获得它。
这是我的 onStart() 和 onStop() 代码 -
@Override
protected void onStart() {
/**
* We are fetching the minimum version that we want the user to have from Firebase Remote Config, only after we have the results we can proceed to start the app.
*/
getMinAppVersion("onStart", () -> {
// navigation drawer
checkValidFacebookSession();
initDrawerMenu();
// network monitoring
registerNetworkReceiver();
// monitoring upload
LocalBroadcastManager.getInstance(this).registerReceiver(mUploadReceiver, new IntentFilter(ULBroadcastConstants.UPLOAD_STATUS_ACTION));
LocalBroadcastManager.getInstance(this).registerReceiver(mFCMReceiver, new IntentFilter(MyFirebaseMessagingService.RECEIVED_FCM_ACTION));
checkInAppUpdate();
});
super.onStart();
}
@Override
protected void onStop() {
getMinAppVersion("onStop", () -> {
mNavigationView.setNavigationItemSelectedListener(null);
mDrawerLayout.removeDrawerListener(mBadgeDrawerToggle);
// network monitor
unregisterNetworkReceiver();
// unregister upload
LocalBroadcastManager.getInstance(this).unregisterReceiver(mUploadReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mFCMReceiver);
});
super.onStop();
}
这是我的 'getMinAppVersion()' 方法 -
private void getMinAppVersion(String didComeFrom, OnRemoteConfigFetchComplete listener){
//fetching the min_version parameter from 'remote config' of Firebase and saves it to our local variable.
FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder().setMinimumFetchIntervalInSeconds(200).build();
mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
mFirebaseRemoteConfig.fetch(0);
mFirebaseRemoteConfig.activate().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
min_version = mFirebaseRemoteConfig.getLong(RemoteConfigUtil.MIN_VERSION);
Timber.tag("min_version_" + didComeFrom).d(String.valueOf(min_version));
if (listener != null)
listener.onFetchComplete();
} else {
Timber.tag("min version").d("error while fetching and activating remove config");
}
});
}
控制台中的远程配置更新不是即时的。它们不像推送通知那样工作。新值仅在 fetch() completes, and you call activate() 返回的 Task 之后生效。我要指出的是,您没有使用 fetch() 返回的任务来查明何时从 Firebase 收到了新值。
这就是它发生的原因。 Remote Config
根据官方文档将值缓存在本地存储中。您可以参考这里并考虑
"Remote Config includes a client library that handles important tasks like fetching parameter values and caching them, while still giving you control over when new values are activated so that they affect your app's user experience. This lets you safeguard your app experience by controlling the timing of any changes."
当您使用客户端库获取远程配置参数时,缓存的值将返回给您(如果有的话)(TL;DL) 更多信息,您可以阅读官方文档here。它使用最小时间间隔来获取值以避免应用程序崩溃,对于该最小间隔时间,最后获取的值用作缓存
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setMinimumFetchIntervalInSeconds(3600)
.build();
mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
这里3600
是minimum interval
的时间间隔,您可以根据自己的需要调整时间。如果您最近调用了 fetch()
并且再次调用它,客户端库会根据 minimum-interval
时间确定应该执行新的 API 调用或提供缓存值。 Firebase 远程配置适用于 REST api,也使用 Throttling。