简体   繁体   English

Firebase方法不起作用

[英]Firebase method doesn't work

I am trying to save date by using this method because I want to get id of object after saved , and use it to save image joining with this object , but this method doesn't work and how do I get the object id? 我尝试使用此方法保存日期,因为我想在保存后获取对象的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")
        }
    })

In this case, you're saving your data as the postRef's immediate child and you'll end up overwriting all the data at the location of postRef. 在这种情况下,您将数据保存为postRef的直接子对象,最终将覆盖postRef位置的所有数据。

Although you've left out a lot of details, in my understanding you could use the following: 尽管您遗漏了许多细节,但据我了解,您可以使用以下内容:

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

In my opinion, requiredId is the Id that you're looking for and your data will be stored at the path specified in postRef . 在我看来, requiredId是您要查找的ID,您的数据将存储在postRef中指定的路径中。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM