Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Firebase远程配置-检查值是否存在_Android_Firebase_Firebase Remote Config - Fatal编程技术网

Android Firebase远程配置-检查值是否存在

Android Firebase远程配置-检查值是否存在,android,firebase,firebase-remote-config,Android,Firebase,Firebase Remote Config,我想在我的应用程序中为一些布尔值实现一种代理。逻辑如下: 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(

我想在我的应用程序中为一些布尔值实现一种代理。逻辑如下:

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
}
  • 我从后端接收一组值
  • 我在Firebase中也设置了一些值
  • 在应用程序中使用值时,我首先检查Firebase中是否存在该值

    3.1。如果存在,则取Firebase值

    3.2。如果它不存在,则采用后端值


  • 问题是-如何检查Firebase远程配置中是否存在该值?

    远程配置已执行此操作,如中所述。您必须提供尚未在控制台中定义的参数。它们完全按照你描述的那样工作,不需要做任何额外的工作。这些默认值将一直使用,直到执行。如果在控制台中定义了该值,则将使用该值而不是默认值。

    我找到了解决方案:

    Firebase Remote Config以
    字符串的形式获取所有值,然后通过方便的方法将它们映射到其他类型,如
    getBoolean()
    getLong()

    因此,可以按如下方式检查布尔配置值是否存在:

    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
    }
    

    其他类型也是如此,例如,firebase上设置为64的
    long
    值将从
    getString()
    返回
    “64”
    ,我发现了另一种方法,可能对其他人在此处登陆有用:

    val rawValue = remoteConfig.getValue(key)
    val exists = rawValue.source == FirebaseRemoteConfig.VALUE_SOURCE_REMOTE
    
    在这种情况下,
    存在
    仅当值从远程返回时才为真(且未设置为默认值或静态提供的值)。所接受的答案是容易出错的,因为不考虑空字符串是从远程

    返回的有效字符串的情况。
    这里的文档用于

    问题是,这些值是动态的。我希望能够在不重新部署应用程序的情况下添加firebase代理值。此外,根据我所看到的,没有义务定义违约。它只是返回该数据类型的默认值(false、“”或0),我想我不明白您在尝试做什么。什么是“firebase代理值”?这些firebase值应该是主值。因此,如果firebase中有一个,请使用它。如果不是,则使用从后端检索的。我希望能够迭代所有后端值,检查firebase中是否有覆盖,如果有,就使用它。我不认为你能够知道控制台中是否有参数值,除非你在那里放置一个特殊值,指示应用程序不使用它。我能问一下这样做的用例是什么吗?您正试图通过此功能提供什么功能?或者,如果有人在远程配置默认值中出错,您是否将其构建为备份?反过来说,如果有人在后端出错,我希望能够快速覆盖用户在其应用程序中获得的值(因为我无法编辑后端,并且通常需要花费大量时间才能在其中进行更改)如果对一个既没有在defaults.xml中定义也没有在远程配置控制台中定义的键执行getString(),会发生什么情况?您将得到“”,这是我在下面发布的解决方案:d这是正确的答案。对于C#:bool exists=rawValue.source==ValueSource.RemoteValue;这里的文档:请注意,只有通过
    setDefaultsAsync()
    定义默认值时,这才有效。当您定义默认值时,
    getString()
    将返回默认值。