Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 777
LegZ Avatar asked Feb 19 '17 14:02

LegZ


2 Answers

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.

like image 95
Bob Snyder Avatar answered Sep 21 '22 00:09

Bob Snyder


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

like image 24
riggaroo Avatar answered Sep 22 '22 00:09

riggaroo