D. Finna D. Finna - 2 months ago 25
iOS Question

How to append without unique auto id firebase swift

I am trying to append data to my database, everytime a user is clicking a like button, if the user hasn't already clicked it. The problem is, when I am appending to the

likesForPost
, a unique id is showing. How can I get rid of that? Otherwise it is not possible for me to check if the user has already clicked the button?

Here is my firebase structure:
enter image description here

It is the green parts that are unique id, the black zones under the unique id are the user id, that I want to be able to check if has already clicked the button.

This is my code for appending to
likesForPost


let quoteString = [userId: true]
let refPhotos = FIRDatabase.database().reference().child("feed-items").child(dataPathen).child("likesForPost")
let refBase54 = refPhotos.childByAutoId()
refBase54.setValue(quoteString)


Hope you guys can help me :-)

Answer

Use :-

let prntRef = FIRDatabase.database().reference().child("feed-items").child(dataPathen).child("likesForPost")
prntRef.observeSingleEventOfType(.Value, withBlock: { (snap) in

    if let favDict = snap.value as? NSMutableDictionary{
        favDict.setObject("true",forKey : uid)
        prntRef.setValue(favDict) 
    } else {
        prntRef.setValue(["true":uid])
    }
})

Or But this is just a HACK!

FIRDatabase.database().reference().child("feed-items").child(dataPathen).child("likesForPost").updateChildValues(quoteString)

For more options check:- http://stackoverflow.com/a/39458044/6297658