senty senty - 5 months ago 18
Swift Question

Getting key value but not the root value of Firebase

I have a node which I am trying to get the key of the value. The node looks like this:

roomMembers
- "user1_user2" (roomname)
- user1: true
- user2: true
- "user1_user3"
- user1: true
- user2: true


I am trying to retrieve and know "user1_user2" and "user1_user3" by both using
.Value
and
.ChildAdded
.

let membersRef = Firebase(url: self.dataURL + "/roomMembers")

membersRef.queryOrderedByChild("user1").observeSingleEventOfType(.Value, withBlock: { snapshot in

snapshot.value // returns the objects as `[String: [String:Bool]]`
snapshot.key // returns 'roomMembers'
})


When I use
.ChildAdded
, it only returns one value

membersRef.queryOrderedByChild("user1").observeEventOfType(.ChildAdded, withBlock: { snapshot in

snapshot.key // returns the right level, but only returns "user1_user2".
snapshot.value // also returns only one value


How can I get "user1_user2" and "user1_user3" (all roomnames) with a single query using
.Value
and
.ChildAdded
?




Edit:

Assume I have a node like this:

lastMessages
- "user1_user2"
- -KJrqHaMzA9Hi-RS_ieV (timestamp'ed id)
- sender
- message
- opponent_seen

- "user1_user3"
- -KJrqHaMzA9Hi-RS_ieB
- sender
- message
- opponent_seen


How can I get timestamped keys and their values?

Answer

To get all room names ...You have to get allKeys like

  snapshot.value.allKeys() 

This will give you ["user1_user2","user1_user3"] i.e your roomnames.

Comments