【问题标题】:Saving a Dictionary of key-values in Firebase using Swift使用 Swift 在 Firebase 中保存键值字典
【发布时间】:2017-10-28 08:50:45
【问题描述】:

此问题来自Post Array to firebase Database in Swift

我正在尝试将一些 ingredients 保存到 recipe 中,然后能够检索其中包含成分的整个食谱。看了上面的问题和这篇博文https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html。我能够重新构建我的数据库

这是我的成分词典:

{
  "IngredientDict": {
    "-KkbWdomTYdpgEDLjC9w": "Eggs",
    "-KkbWvaa5DT1PY7q7jIs": "Spaghetti",
    "-KkbaDQ8wYJxR3O2OwKd": "Parmesan"

      // More possible ingredients

  }
} 

这是我的成分数据:

{
  "IngredientData": {
    "ingredients": {
      "-KkbWdomTYdpgEDLjC9w": {
        "name": "Eggs",
        "uid": "-KkbWdomTYdpgEDLjC9w"
      },
      "-KkbWvaa5DT1PY7q7jIs": {
        "name": "Spaghetti",
        "uid": "-KkbWvaa5DT1PY7q7jIs"
      },
      "-KkbaDQ8wYJxR3O2OwKd": {
        "name": "Parmesan",
        "uid": "-KkY90e7dAgefc8zH3_F"

      // More possible ingredients

      }
    }
  }
}

我现在想一次性将这些成分添加到我的食谱中。这就是我目前制作食谱的方式:

@IBAction func addRecipe(_ sender: Any) {

        guard let itemNameText = recipeName.text else { return }

        guard itemNameText.characters.count > 0 else {

            print("Complete all fields")
            return
        }

        let recipeKey = databaseRef.child("RecipeData").child("recipe").childByAutoId().key
        let recipeItem: [String : Any] = ["recipeName" : itemNameText, "recipeID" : recipeKey]
        let recipe = ["\(recipeKey)" : recipeItem]

        databaseRef.child("RecipeData").child("recipe").updateChildValues(recipe)

        print("\(itemNameText) was added")
    }

这是结果:

{
  "RecipeData": {
    "recipe": {
      "-Kkv36b_aPl7HAO_VYaJ": {
        "recipeName": "Spaghetti Carbonara",
        "recipeID": "-Kkv36b_aPl7HAO_VYaJ"
      }
    }
  }
}

如何添加成分?目前我可以在 Firebase 上手动添加它。但我希望能够采取行动,用配料保存整个​​食谱。大多数示例给出了一个带有一组声明属性的字典。但是我的可以是随机的,因为每个食谱可以有随机数量的成分。

【问题讨论】:

  • 您想一次性存储配料和食谱,对吗?在此查询中,databaseRef.child("RecipeData").child("recipe").updateChildValues(recipe)
  • 正确,目前只存储一个配方。我不知道如何储存这些成分。
  • Firebase 上已经存在“IngredientData”:{“ingredients”:{ data } } 吗?
  • 是的,向您展示我是如何创建这个的会有帮助吗?
  • 一些事情:首先,密钥在成分节点中存储了两次——一次作为密钥,一次在子节点中。这可能是不必要的,只是存储是关键:成分名称。第二件事,这是一个设计选择,uid通常表示用户id。成分不是用户,因此为了便于阅读,您可能希望使用 uid 以外的其他内容,也许是 recipeId。但是,如果您将每种成分简化并存储为键:成分名称对,这可能又是一个静音点。最后一件事是成分->成分太深了。只需将您的成分直接存储在成分节点中即可。

标签: ios swift dictionary firebase firebase-realtime-database


【解决方案1】:

在你的 addRecipe 函数中,试试这个:

...
let ingredients: [String] = ...
var ingredientsJSON = [String: Bool]()
for key in ingredients {
    ingredientsJSON[key] = true
}
let recipeJSON: [String : Any] = [
    "recipeName" : itemNameText,
    "recipeID" : recipeKey,
    "ingredients": ingredientsJSON
]
let recipe = ["\(recipeKey)" : recipeJSON]
databaseRef.child("RecipeData").child("recipe").updateChildValues(recipe)
...

【讨论】:

  • 目前 ingredientText 只是来自 textField 出口的字符串。所以实际上是没有必要的。我将更新我的问题并删除它。我现在如何在我的食谱中存储成分字典?
  • 我认为最好的解决方案是手动将配料添加到配方中,例如"-KkbWdomTYdpgEDLjC9w" : true。那你知道我怎么得到-KkbWdomTYdpgEDLjC9w这个名字吗?
  • 我不确定你的意思。我可以向您展示如何手动将它们添加到配方中。但目前我的 addRecipe 函数与我的配料没有任何关系。
  • 成分变量在Recipe类中
  • 对不起,我解释的不够清楚。如果是这种情况,请告诉我。
猜你喜欢
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多