Firebase远程配置初始加载默认值

时间:2019-08-21 05:25:27

标签: android firebase firebase-remote-config

    public class RemoteConfigManagerImpl implements RemoteConfigManager {
    private FirebaseRemoteConfig mFirebaseRemoteConfig;
    private int mCacheExpiration = 3600;

    @Inject
    public RemoteConfigManagerImpl() {
        mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
        FirebaseRemoteConfigSettings settings = new FirebaseRemoteConfigSettings.Builder()
                .setDeveloperModeEnabled(BuildConfig.DEBUG).build();
        mFirebaseRemoteConfig.setConfigSettings(settings);
        mFirebaseRemoteConfig.setDefaults(R.xml.remote_config);
        fetchRemoteConfig();
    }

    @Override
    public void fetchRemoteConfig() {
        // if developer mode is enabled set cache to 0 to instantly see updates in remote config
        if (BuildConfig.DEBUG)
            mCacheExpiration = 0;

        mFirebaseRemoteConfig.fetch(mCacheExpiration)
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {
                        mFirebaseRemoteConfig.activateFetched();
                    }
                }).addOnFailureListener(e -> e.printStackTrace());
    }

    @Override
    public Object getRemoteConfig(DataObject dataObject) {
        return dataObject.getDataFromRemoteConfigObject(mFirebaseRemoteConfig);
    }
}
  

我有用于Firebase远程配置的此类。我需要在另一活动中使用远程配置值。但是我发现有一个异步任务,调用 mFirebaseRemoteConfig.activateFetched()将花费一些时间。 >;

  private boolean isDisplayPriceArea() {
    Object remoteConfig;
    boolean value = false;
    remoteConfig = remoteConfigManager.getRemoteConfig(new DataObject(new DisplayPriceAreaTest()));
    if (remoteConfig instanceof DisplayPriceArea) {
        value = ((DisplayPriceArea) remoteConfig).isDisplayPriceArea();
    }
    return value;
}
  

这是我在活动中调用的方法。第一次我总是得到默认值,而不是远程值。在 mFirebaseRemoteConfig.activateFetched(); 调用后,如何通知此方法。   感谢您的光临!

0 个答案:

没有答案