Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I want to implement kind of a proxy for some boolean values in my app. The logic would be as follows:
I receive a set of values from my back-end
I have some of these values set in Firebase as well
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, i.e. a long value set to 64 on firebase will be returned from getString() as "64".
–
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
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.
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.