0

Maybe I am missing something, but shouldn't the fetch() be called only if the cached values are older than the cached value?

Having this code called from activity's onCreate

firebaseRC = FirebaseRemoteConfig.getInstance()

firebaseRC.fetch(3600L). .addOnCompleteListener(this, new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    //THE TASK IS ALWAYS SUCCESSFUL
                    firebaseRC.activateFetched();
                }  
            }
        });

If I start the app on my emulator a few times in a row, the fetch completes successfully each time.

Shouldn't the fetch actually complete with success only if my data is older than 3600 seconds? So starting the app a second time, fetch should complete without onComplete

The docs say:

Remote Config caches values locally after the first successful fetch. By default the cache expires after 12 hours, but you can change the cache expiration for a specific fetch by passing the desired cache expiration to the fetch method. If the values in the cache are older than the desired cache expiration, Remote Config will request fresh config values from the service. If your app requests fresh values using fetch several times, requests are throttled and your app is provided with cached values.

It doesn't say how onComplete will trigger... should I use activateFetched() each time as it has the same value?

Alin
  • 14,809
  • 40
  • 129
  • 218

1 Answers1

1

The API documentation for fetch() says that the results will come from either cache or server. It doesn't say that a cached result it considered a failure. The fetch will only fail if it can't give you any results at all, which means your app is probably offline and has no previously successful fetched values in cache.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thank you very much for your time and answer. So this means, that if I set a default cache value of 6 hours, then each 6 hours the `fetch` will check with the server, otherwise it will use the cached value? In this case, isn't a problem to call `fetch` and on `onComplete` apply `activateFetched()` each time the app is started? – Alin Aug 22 '18 at 19:40
  • 1
    I can't tell you if calling activateFetched() is going to be a problem for you. It depends on how you intend to use the values. You should activate only when you are comfortable replacing with the possibility of replacing all the values at the moment it's invoked. – Doug Stevenson Aug 22 '18 at 19:49
  • I understand what you mean, but I think I am missing somethig. What I am trying to say is, let's say that my remote config parameter `myParam` changes once a month. This means that each day I will call `activateFetched()` with the same cached value over and over again considering that `firebaseRemoteConfig.getString(myParam)` should actually return the same value, even without using the `activateFetched` – Alin Aug 22 '18 at 19:58
  • I'm not sure what you're asking. The only thing you need to know is that activating fetched values will make them available to your code via getString, etc. If you don't activate, they will never appear in your app. If you activate with no new fetched values, then your app will see no changes. – Doug Stevenson Aug 22 '18 at 20:04
  • Sorry, I may not explain it well. Let's say that I want to use the values as soon as I get them, so activate them when received. My string param value changes each 30 days, this means that my app could live ok with the initial `fetch` for 30 days. Since on `onComplete` is triggered for `cached` or `fresh` values, I need to `activateFetched()` each time I start the app because I don't know what values are returned. This means that I `activateFetched` with the same value for 30 days. My thought was that there should be no need to call `activateFetched` if the values are the same as cached ones. – Alin Aug 22 '18 at 20:12
  • 1
    You won't be able to tell if there are new values or not. You should probably activate every time. – Doug Stevenson Aug 22 '18 at 20:13