获取键值但不是Firebase的根值

时间:2016-06-10 04:39:27

标签: ios swift firebase firebase-realtime-database

我有一个节点,我试图获取值的关键字。节点如下所示:

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

我正在尝试使用.Value.ChildAdded检索并了解“user1_user2”和“user1_user3”。

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'
})

当我使用.ChildAdded时,它只返回一个值

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

如何使用.Value.ChildAdded的单个查询获取“user1_user2”和“user1_user3”(所有会议室名称)?

编辑:

假设我有一个这样的节点:

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

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

如何获取带时间戳的密钥及其值?

1 个答案:

答案 0 :(得分:2)

获取所有房间名称......您必须 allKeys ,如

  snapshot.value.allKeys() 

这会给你["user1_user2","user1_user3"],即你的房间名。