简体   繁体   中英

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?

    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.

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 .

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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