7
votes

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?

4

4 Answers

7
votes

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.

3
votes

The callbacks for Firebase Remote Config have been designed like that, it will return the cached values first. If there is no cached value saved from the server, it will return the value defined in defaults and trigger a remote fetch. The next time it returns it will return the fetched values from the server if it manages to save them.

The way in which Firebase Remote Config decides on a value can be described as follows:

First it checks if there is a cached value that was stored from the server, if there is it uses that and will return that value on the first call.

If there is no cached value, it looks to the defaults defined either programmatically or in the defaults file. (When you call setDefaults())

If there is no value cached from the server, and no value in defaults, it uses the system default for that type.

More info can be found here : https://firebase.google.com/docs/remote-config/ Firebase Remote Config defaults

2
votes

Like @Bob Snyder pointed out, this is because of the async nature of firebase.

So use onCompleteListener like this to fix the issue:

firebaseRemoteConfig.activate().addOnCompleteListener {
    //logic to check the remote value
}
1
votes

One issue that I was running into when fetching the RemoteConfig from an Android device was that we were initially using the method

fetch()

which gave us the same issue where the initial value was always the same as the default. Changing this to

fetchAndActivate()

fixed the issue for us. I assume the difference is that Firebase allows you to fetch the data but not immediately 'activate' it, which presumably is helpful if you want to take some immediate action based on your default values, then activate the remote values and then any logic after that point would be based on the remote values.

Hope this helps someone :)