简体   繁体   中英

Firebase Remote Config - check if value exists

I want to implement kind of a proxy for some boolean values in my app. The logic would be as follows:

  1. I receive a set of values from my back-end
  2. I have some of these values set in Firebase as well
  3. When using a value in the app, I first check if it exists in Firebase

    3.1. If it exists, take the Firebase value

    3.2. If it does not exist, take the backend value

The question is - how can I check if the value exists in Firebase Remote Config?

I found another way, maybe useful for someone else landing here:

val rawValue = remoteConfig.getValue(key)
val exists = rawValue.source == FirebaseRemoteConfig.VALUE_SOURCE_REMOTE

In this case exists will be true only if the value is returned from remote (and and has not been set as default or static provided value). The accepted answer is error prone as does not consider the case where the empty String is a valid String returned from remote

Here the docs for FirebaseRemoteConfigValue

I have found the solution:

Firebase Remote Config fetches ALL values as Strings and only then maps them to other types in convenience methods such as getBoolean() , getLong() etc.

Therefore, a boolean config value existence can be checked as follows:

String value = firebaseRemoteConfig.getString("someKey");

if(value.equals("true")){
    //The value exists and the value is true
} else if(value.equals("false")) {
    //The value exists and the value is false
} else if(value.equals("")) {
    //The value is not set in Firebase
}

Same goes for other types, ie a long value set to 64 on firebase will be returned from getString() as "64" .

Remote Config already does this, as described in the documentation . You're obliged to provide default values for parameters that haven't been defined in the console. They work exactly as you describe, without having to do any extra work. These defaults will be used until you perform a fetch . If the value is defined in the console, then it will be used instead of the default.

Firebase Remote Config (FRC), actually provides 3 constants to know from where is the value retrieved with getValue(forKey) ( documentation )

  • VALUE_SOURCE_DEFAULT --> value from default set by user
  • VALUE_SOURCE_REMOTE --> value from remote server
  • VALUE_SOURCE_STATIC --> value returned is default (FRC don't have the key)

Knowing this, you can do a "wrapper" like this:

class FirebaseRemoteConfigManager {
.....

    override fun getBoolean(forKey: String): Boolean? = getRawValue(forKey)?.asBoolean()

    override fun getString(forKey: String): String? = getRawValue(forKey)?.asString()

    override fun getDouble(forKey: String): Double? = getRawValue(forKey)?.asDouble()

    override fun getLong(forKey: String): Long? = getRawValue(forKey)?.asLong()

    private fun getRawValue(forKey: String): FirebaseRemoteConfigValue? {
        val rawValue = remoteConfig.getValue(forKey)
        return if (rawValue.source == FirebaseRemoteConfig.VALUE_SOURCE_STATIC) null else rawValue
    }
...
}

If you get a null , you know the key don't exist in FRC. Take care of using default values as "not exist", because maybe in your FRC you want to set this value, and you will have a false positive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM