Swift 使用子字典设置 firebase 字典的值

标签 swift firebase dictionary firebase-realtime-database setvalue

我有一个对象,其中有一个子对象数组。我想将这个对象原样保存到 firebase 数据库中。我怎样才能快速完成这个保存过程?

我尝试创建新词典

-LQTDo9OU9zw84IKnnW3
     code: "111"
     date: "4/11/2018"
     dateAndTime: "2018/11/4/12/18"
     description: "Description"
     editor: "Burak Akdeniz"
     predictions
          -0
            prediction: "Maç Sonucu 1"
            predictionRatio: "2"
    startTime: "12 : 18 PM"
    status: "Waiting"



let newMatchRef = refMatches.child("platinium").childByAutoId()
            let m : Dictionary<String, AnyObject> = [
            "code": match.code as AnyObject,
            "date": match.date as AnyObject,
            "dateAndTime": match.dateAndTime as AnyObject,
            "description": match.description as AnyObject,
            "editor": match.editor as AnyObject,
            "league": match.league as AnyObject,
            "matchType": match.matchType as AnyObject,
            "predictions": , //<--- HERE I have a predictions array includes prediction objects. This is the point I get error.
            "startTime": match.startTime as AnyObject,
            "status": match.status as AnyObject,
            "team1": match.team1 as AnyObject,
            "team2": match.team2 as AnyObject]
            print(newMatchRef)
            newMatchRef.setValue(m)

我需要将该对象及其子节点保存到 firebase 数据库

最佳答案

Firebase 的“关键”是始终将父节点和子节点视为键:值对。键始终是字符串,但值可以是字符串、数字、 bool 值和数组。

在这种情况下,您基本上做对了,只需要将数组本身视为一个值即可。

让我们用两个元素定义该数组(每个元素都是一个字典)

    let myArray = [
        ["prediction": "pred 0",
         "predictionRatio": "2"],

        ["preduction": "pred 1",
         "predictionRatio": "3"]
    ]

然后,让我们创建一个字典对象,其中包含键:值对并将整个内容写入 firebase。

    let dict:[String: Any] = [
        "code": "1111",
        "date": "04/11/2018",
        "description": "Description",
        "predictions": myArray
    ]

然后将其写入使用 .childByAutoId 创建的引用

    let ref = self.ref.child("some_node").childByAutoId()
    ref.setValue(dict)

some_node 中的结果如下所示

child_by_auto_id_0
   code: "1111"
   date: "04/11/2018"
   description: "some description"
   predictions:
      0:
         "prediction": "pred 0"
         "predictionRate": "2"
      1:
         "prediction": "pred 1"
         "predictionRate": "3"

请注意,我告诉字典它将获取 String: Any 的键值对。如上所述,键始终是字符串,但在这种情况下,一些值是字符串,然后是数组。 'Any' 类型可以处理这个问题。

也就是说,通常最好避免在 Firebase 中使用数组。与它们一起工作具有挑战性,不容易维护并且查询很好......它们只是不喜欢被查询。最好使用相同的 .childByAutoId 方法生成数组的键。它要灵活得多。

   predictions:
      auto_id_0:
         "prediction": "pred 0"
         "predictionRate": "2"
      auto_id_1:
         "prediction": "pred 1"
         "predictionRate": "3"

关于这一点,如果您每个人都想查询它们,您可能需要考虑对预测节点进行非规范化

child_by_auto_id_0
       code: "1111"
       date: "04/11/2018"
       description: "some description"

predictions
      auto_id_0:
         "prediction": "pred 0"
         "predictionRate": "2"
         "parent_node_ref": "child_by_auto_id_0"
      auto_id_1:
         "prediction": "pred 1"
         "predictionRate": "3"
         "parent_node_ref": "child_by_auto_id_0"

关于Swift 使用子字典设置 firebase 字典的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53946296/

相关文章:

ios - Realm 数据库大小

swift - NSCollectionView 的 didScroll

ios - FirebaseApp.configure() 在启动时使应用程序崩溃

c# - 在多线程环境中返回 C# 中的字典

c# - 从数组列表中获取字典

ios - 如何在不使用导航 Controller 的情况下推送 UIViewController?

ios - 使用 NavigationViewController 更改 View

android - 在不保留信息的情况下保持 Firebase 同步状态

ios - Firebase - iPhone X 上的 iOS 应用安装不起作用

python - 如何从Python3中的输入字符串创建字典