Ahad Sheriff Ahad Sheriff - 3 months ago 141
Swift Question

Swift: Trouble setting value of data using Firebase

The database in my firebase database is currently saved in this manner:

{
"messages" : {
"-KOxK41dRvVOjXq0CgZ0" : {
"date" : "2016-08-12 05:58:12 +0000",
"location" : "Seattle, WA",
"score" : 0,
"senderId" : "LylxZpPcmQgSFLGmpemKJDHvqQX2",
"text" : "Yo"
}
}
}


I am trying to implement a way for a user to press a button and the score will go up. I thought I could just use Firebase's
setValue
code to just add one to the score value for each message.

Here is the code that I used for that:

super.collectionView(collectionView, didTapMessageBubbleAtIndexPath: indexPath)
let data = self.messages[indexPath.item]
print("They tapped: " + (data.text) + "- " + (data.senderDisplayName))
data.score += 1
messageRef.child("messages.score").setValue(data.score)
print(data.score)


However I am getting the following error:

*** Terminating app due to uncaught exception 'InvalidPathValidation', reason: '(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''


I am assuming this is because I have "messages.score" as my child when I should be putting something else in there. I looked through the Firebase documentation but couldn't figure out what to put.

Does anybody have any idea how I can fix my problem?

Thanks!

Edit:

I tried this piece of code:

var rootRef : FIRDatabaseReference = FIRDatabase.database().reference()
let senderID = data.senderId
rootRef.child("messages").observeEventType(.Value, withBlock: { (snap) in
if snap.exists(){
for each in snap as! [String:AnyObject]{
let postID = each.key as! String
if let messagesDict = each.value as! [String:AnyObject]{
if senderID == messageDict["senderId"]{
//Checking for the senderID of the user so that you only increment the score of that particular message post
let userScore = messageDict["score"] as! Int
userScore = userScore + 1
rootRef.child("messages").child(postID).child("score").setValue(userScore)
}
}
}
}
})


But am getting the following errors in the editor:

errors

Answer

Use :-

    let rootRef : FIRDatabaseReference = FIRDatabase.database().reference()
    var senderID = FIRAuth.auth()!.currentUser!.uid

    rootRef.child("messages").observeEventType(.Value, withBlock: { (snap) in

        if snap.exists(){
            if let messagesDict = snap.value! as? [String : AnyObject]
            {
                for each in messagesDict as [String : AnyObject]{

                    let postID = each.0
                    if let messageDict = each.1 as? [String:AnyObject]{
                        if senderID == messageDict["senderId"] as! String{
                            //Checking for the senderID of the user so that you only increment the score of that particular message post
                            var userScore = messageDict["score"] as! Int
                            userScore = userScore + 1
                            rootRef.child("messages").child(postID).child("score").setValue(userScore)
                        }
                    }                   
                }
            }
        }
    })
Comments