【问题标题】:Firebase Remote Config setDefaults: doesn't store NSDictionaryFirebase 远程配置 setDefaults:不存储 NSDictionary
【发布时间】:2020-04-26 10:40:20
【问题描述】:

看起来setDefaults: 没有正确解析和存储嵌套的 NSDictionary 对象。

这是一些简单的测试代码:

FIRRemoteConfig *remoteConfig = [FIRRemoteConfig remoteConfig];

NSDictionary *defaultValues = @{
    @"stringTestKey": @"stringValue",
    @"numberTestKey": @10,
    @"dictionaryTestKey": @{
            @"nestedKey":@"nestedValue"
    }
};

NSLog(@"default dict > %@", defaultValues);

[remoteConfig setDefaults:defaultValues];

NSString *stringTest = [remoteConfig defaultValueForKey:@"stringTestKey"].stringValue;
NSNumber *numberTest = [remoteConfig defaultValueForKey:@"numberTestKey"].numberValue;
NSDictionary *dictionaryTest = [remoteConfig defaultValueForKey:@"dictionaryTestKey"].JSONValue;

NSLog(@"String value > %@", stringTest);
NSLog(@"Number value > %@", numberTest);
NSLog(@"Dictionary value > %@", dictionaryTest);

前两个NSLog 正确返回stringValue10,但第三个简单返回null

是我做错了什么还是当前的 SDK 坏了?

【问题讨论】:

  • 您希望它记录什么,为什么?
  • 我在代码示例下方评论:第三个 NSLog 应该返回一个 NSDictionary,它是 dictionaryTestKey 的值。相反,它返回(null)
  • 我也在 Firebase 文档提供的 Objective-C 演示项目中测试了相同的行为:当一个具有字典值类型的新键添加到演示中包含的 plist 时,defaultValueForKey键返回(null)
  • 如果我正确阅读了文档,JSONValue 会将配置字段的值解析为 JSON。这意味着它可能需要一个字符串作为输入,这就是您表示 JSON 的方式。
  • 如果您认为有错误,请在 GitHub 上提交错误报告。 github.com/firebase/firebase-ios-sdk

标签: ios objective-c firebase firebase-remote-config


【解决方案1】:

不知道你是否找到了解决方案,但我现在遇到了同样的问题,经过一段时间的挖掘,你需要将它保存为String类型而不是Dictionary,我使用plist文件来获取默认值,只需添加新键dictionaryTestKey,并将类型设置为String,将值设置为类似

{   “url”: "https://google.com”,   "nestedKey": "nestedValue" } 然后使用setDefaults 保存默认值, 当你需要检索它时,使用

let data = RemoteConfig.remoteConfig().configValue(forKey: "dictionaryTestKey").dataValue let result = try? JSONDecoder().decode(YourDecodableStruct.self, from: data)

你会得到你想要的所有信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多