How to implement fetch() from Firebase Remote Config in onStart() method?

南笙酒味 提交于 2019-12-07 01:46:39

问题


I'm trying to implement calling Firebase Remote Config fetch() method in onStart(). I thought it would be quite easy but after few attempts it isn't.

First of all, I want to check for new config values as soon as the user opens the app and cache expiration time was exceeded. That's why I chose onStart() method to do this.

At first I used standard approach:

//called in Activity onStart() method
public static void fetchRemoteConfigValues(Activity activity) { 
        long cacheExpirationInSeconds = 43200L;
        firebaseRemoteConfig.fetch(cacheExpirationInSeconds)
                .addOnSuccessListener(activity, new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        LOGD(TAG, "onSuccess: Fetch Succeeded");
                        FirebaseRemoteConfig.getInstance().activateFetched();

                    }
                })
                .addOnFailureListener(activity, new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        LOGE(TAG, "onFailure: Fetch Failed", e);
                    }
                });
}

Everything would work correct except that there is a situation when cache expiration time is exceeded and there is no Internet connection so onFailure is called. And now my question is:

  1. Why is this failure request increases throttle counter?

After few fetch() calls with no Internet FirebaseRemoteConfigFetchException turns in FirebaseRemoteConfigFetchThrottledException. And now even if Internet is available again I can't fetch data from the server because of throttle limit on.

My temporary workaround is checking Internet availability before calling fetch():

public static void fetchRemoteConfigValues(Activity activity) { 
    if (NetworkHelper.isNetworkAvailable(activity))
        long cacheExpirationInSeconds = 43200L;
        firebaseRemoteConfig.fetch(cacheExpirationInSeconds)
        (...)
    }
}

But it can cause some problems when connection is established but still no Internet / no server response.

  1. Is there a better solution / approach to my problem?

来源:https://stackoverflow.com/questions/44091088/how-to-implement-fetch-from-firebase-remote-config-in-onstart-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!