askaale askaale - 2 months ago 21
Swift Question

Swift Firebase snapshot.allValues update

I am currently following a course on Udemy that teaches how to create a chat application with Firebase. However, I finished this course a couple of weeks ago, and then suddenly came the Swift 3.0 update. I am now struggling to convert this line in to Swift 3 Firebase:

firebase.child("Recent").queryOrderedByChild("chatRoomID").queryEqualToValue(chatRoomID).observeSingleEventOfType(.Value) { (snapshot:FIRDataSnapshot) in


var createRecent = true

if snapshot.exists() {
for recent in snapshot.value!.allValues {
if recent["userId"] as! String == userId {
createRecent = false
}
}
}

if createRecent {

CreateRecentItem(userId, chatRoomID: chatRoomID, members: members, withUserUsername: withUserUsername, withUserUserId: withUseruserId)


}

}


And I tried to do this:

firebase.child("Recent").queryOrdered(byChild: "chatRoomID").queryEqual(toValue: chatRoomID).observeSingleEvent(of: .value) { (snapshot:FIRDataSnapshot) in

var createRecent = true

if snapshot.exists() {

if let values = snapshot.value as? [String:AnyObject] {

for recent in values {
if recent["userId"] as! String == userId {

}
}
}

//}

}

}

}


But of course this returned an error. Any ideas on how I would solve this particular code-convert?

Thanks in advance.

Answer

Try using:-

firebase.child("Recent").queryOrdered(byChild: "chatRoomID").queryEqual(toValue: chatRoomID).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in

        var createRecent = true

        if snapshot.exists() {

            if let values = snapshot.value as? [String:AnyObject] {

                for recent in values {
                    if let userId = recent.value["userId"] as? String{

                      }
                }
            }
        }

    })
Comments