1

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 :-)

D. Finna
  • 467
  • 2
  • 7
  • 19

1 Answers1

1

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:- https://stackoverflow.com/a/39458044/6297658

Community
  • 1
  • 1
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • The problem could be fixed if there was some way to get the unique auto id, firebase is creating? – D. Finna Sep 14 '16 at 13:56
  • Doesnt matter until your uid doesn't exist in the dictionary key, it will act as appending the values – Dravidian Sep 14 '16 at 14:01
  • Great! Do you also know how to log out of facebook, so I can try another account in the simulator? (I have a logout button, but when I press log in again, it is using the same account) – D. Finna Sep 14 '16 at 14:03