【发布时间】:2017-05-11 14:28:42
【问题描述】:
我正在尝试在一个孩子中创建多个孩子。我目前可以在我的食谱中创建这个:
{
"RecipeData": {
"recipe": {
"-KjTSH4uPQ152Cr-hDok": {
"name": "Cook rice",
"ID": "-KjTSH4uPQ152Cr-hDok",
}
}
}
}
使用:
let recipe: [String : Any] = ["name" : self.recipe.name,
"ID" : self.recipe.key]
配方的类如下所示:
class Recipe {
var name: String!
var key: String
init(from snapshot: FIRDataSnapshot) {
let snapshotValue = snapshot.value as! [String: Any]
self.name = snapshotValue["name"] as! String
self.key = snapshot.key
}
}
但我现在想创建另一个子数组,它们将在“方法”中,看起来像这样。
{
"RecipeData": {
"recipe": {
"-KjTSH4uPQ152Cr-hDok": {
"name": "Cook rice",
"ID": "-KjTSH4uPQ152Cr-hDok",
"method": [
{
"step 1": "instruction 1"
},
{
"step 2": "instruction 2"
},
{
"step 3": "instruction 3"
}
]
}
}
}
}
编辑:
食谱是这样更新的
databaseRef.child("RecipeData").child("recipe").updateChildValues(recipe)
我看过Firebase How to update multiple children?,它是用javascript编写的,但不知道如何实现它。如果有更好的问题或示例,请随时告诉我。
【问题讨论】:
-
显示使用完整的结构,你在其中添加了这个名字和 id 的父级
-
@NiravD 请查看我的更新结构
-
您是否有要在其中添加此数组的配方 ID?
-
否,配方 ID 已添加到配方中。
-
我的意思是你怎么知道你现在需要在哪个配方中通过更新来添加这个方法,或者只有一个配方?
标签: ios swift firebase firebase-realtime-database