使用Swift在Firebase中保存键值字典

时间:2022-02-17 21:25:06

This question is a follow up from: Post Array to firebase Database in Swift

这个问题是在Swift中从:Post数组到firebase数据库的后续问题

I am trying to save some ingredients into a recipe and later be able to retrieve the entire recipe with the ingredients inside it. After looking at the question above and this blog post https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html. I was able to re-structure my database

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

This is my Ingredient Dictionary:

这是我的成分字典:

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

      // More possible ingredients

  }
} 

This is my Ingredient Data:

这是我的成分数据:

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

      // More possible ingredients

      }
    }
  }
}

I now want to add these set of ingredients to my recipe in one action. This is how I currently make a recipe:

现在我想在我的食谱中加入这些配料。这就是我现在制作食谱的方法:

@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")
    }

This is the outcome:

这是结果:

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

How do I add the ingredients? At the moment I can manually add it on the Firebase. But I would like to be able have an action that saves the entire recipe with the ingredients. Most examples give a dictionary with a set of declared properties. However mine can be random as each recipe can have random number of ingredients.

如何添加原料?现在我可以在Firebase上手动添加它。但我希望能有一个行动,拯救整个食谱与配料。大多数示例给出带有声明属性集的字典。但是我的食谱可以是随机的,因为每个食谱都有随机的配料。

1 个解决方案

#1


1  

In your addRecipe function, try this:

在你的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)
...

#1


1  

In your addRecipe function, try this:

在你的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)
...