我正在尝试使用此方法保存日期,因为我希望在保存后获取对象的id,并使用它来保存与此对象的图像连接,但这种方法不起作用,我如何获得对象id?
postRef.setValue("I'm writing data", withCompletionBlock: {
(error, ref) in
if (error != nil) {
Constant.displayAlert(view: self, title:"", Message: "RRRR")
} else {
Constant.displayAlert(view: self, title:"", Message: "RRRR")
}
})
答案 0 :(得分:2)
在这种情况下,您将数据保存为postRef的直接子项,并且最终会覆盖postRef位置的所有数据。
虽然您遗漏了很多细节,但根据我的理解,您可以使用以下内容:
insertionRef = postRef.childByAutoId() // inserts a child by an auto generated Id
requiredId = insertionRef.key // returns a string
insertionRef.setValue("I'm writing data", withCompletionBlock: {
(error, ref) in
if (error != nil) {
Constant.displayAlert(view: self, title:"", Message: "RRRR")
} else {
Constant.displayAlert(view: self, title:"", Message: "RRRR")
}
})
在我看来, requiredId 是您要查找的ID,您的数据将存储在 postRef 中指定的路径中。
希望这有帮助。