我想像这样保存我的数据:
- 父级
- 1
- 值(value)在这里
- 另一个值(value)
- 2
- 值(value)在这里
- 另一个值
等等。现在我正在使用它向 Parent
数组添加一个项目:
func save() {
let ref = AppDatabaseReference.users(uid: uid).reference()
ref.child("parent").childByAutoId().setValue(toDictionary())
}
item.save()
这可行,但它将每个项目保存在唯一的字符串下(如预期的那样)。但这让我更难引用一些东西。
- 父级
- dhsaifhafh86
- 值(value)在这里
- 另一个值(value)
- dhsaifusfh53
- 值(value)在这里
- 另一个值
我想过在 toDictionary()
周围添加方括号,例如 ref.child("parent").setValue([toDictionary()])
但随后它只是替换父级下的现有子级而不是添加新子级:
- 父级
- 更换的东西
- 你永远不会赢!
如果有帮助的话,toDictionary()
看起来像这样:
func toDictionary() -> [String: Any] {
return [
"title": title,
"description": description,
"stuff": stuff,
"more stuff": moreStuff
]
}
最佳答案
我做了这样的事情-
let ref=firebase().database().ref("Parent");
ref.once("value").then(snap=>{
let length=snap.val()?Object.keys(snap.val()).length:0;
ref.update({[length+1]:{Value here,Another value}});
});
关于swift - 将 Firebase 中的数据保存在编号列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48567583/