Swift 设置带有子词典的 firebase 词典的值

Swift set a value of firebase dictionary with sub dictionaries

我有一个对象,其中有一个子对象数组。我想将这个对象原样保存到 firebase 数据库中。我如何在 swift 中执行此保存过程?

我尝试创建新词典

-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 数据库

我假设其余的都适用于当前的实现,如果您将预测行替换为:

"predictions": match.predictions as [[String: String]]

如果上面的代码没有意义或不起作用,请编辑您的问题并显示预测数组和预测数据结构。

Firebase 的'key' 始终将父节点和子节点视为键:值对。键始终是字符串,但值可以是字符串、数字、布尔值和数组。

在这种情况下,你几乎做对了,只需要考虑数组本身就是一个值。

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

    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"

请注意,我告诉字典它将获取字符串的键:值对:任意。如上所述,键始终是字符串,但在这种情况下,一些值是字符串,然后是数组。 '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"