Sam
Sam

Reputation: 243

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

Upvotes: 1

Views: 135

Answers (1)

Anmol Arora
Anmol Arora

Reputation: 109

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.

Upvotes: 2

Related Questions