Search code examples
androidfirebasefirebase-remote-config

Firebase Remote Config - Initial fetch return local default values


I'm using Firebase Remote Config to fetch remote data and my app needs an up-to-date data from the first launch.

I'm doing a fetch and update in my Application's onCreate():

mFirebaseRemoteConfig.fetch(cacheExpiration)
    .addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                mFirebaseRemoteConfig.activateFetched();
            }
        }
    });

And read the value with :

myValue = mFirebaseRemoteConfig.getBoolean(Constants.FIREBASE_REMOTE_MY_VALUE);
  1. The first fetch works well (activateFetched() is successfully triggered), but it returns the remote_config_defaults value and not the published remote config.
  2. The second fetch, even a few seconds later, returns the remote value.
  3. After that, the following fetches are subject to the cacheExpiration rule (which is totally OK).

Any idea why my remote value is not fetched at the first call?


Solution

  • It sounds like you are overlooking the asynchronous nature of fetching the remote parameters. The onComplete() callback fires after a request to the Firebase servers is sent and the reply received. This will take a fraction of a second, maybe more.

    If your statement to use the fetched value:

    myValue = mFirebaseRemoteConfig.getBoolean(Constants.FIREBASE_REMOTE_MY_VALUE);
    

    follows the call to fetch() and is not in the onComplete() callback, it will execute before the config data has been received. The second call only appears to work because enough time has elapsed for the first call to complete and the data it fetched and activated is present.