查询Firebase以嵌套子swift

时间:2016-08-05 02:45:22

标签: ios swift firebase firebase-realtime-database

我需要帮助在Firebase中获取嵌套数据。

以下是我在Firebase中构建数据的方式:

- Meetups
 - UniqueID
   - address: "xxxx"
   - date: "xxxx"
   - creator: "xxxx"
   - invitedFriends
      - 1: "xxxx"
      - 2: "xxxx"
      - 3: "xxxx"

我正在试图弄清楚如何检索inviteFriends数据。

目前我以这种方式检索数据:

BASE_URL.child("meetups").observeEventType(.Value, withBlock: { snapshot in

  self.meetups = []


  if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

    for snap in snapshots {

      if let postDictionary = snap.value as? Dictionary<String, AnyObject> {
        let key = snap.key

        let meetup = Meetup(key: key, dictionary: postDictionary)

        self.meetups.insert(meetup, atIndex: 0)

      }
    }

这允许我从地址,日期和创建者创建我的Meetup类的实例。但是,我最近将inviteFriends数据添加到结构中,我试图弄清楚如何在同一个过程中查询它。谢谢你的任何建议!

1 个答案:

答案 0 :(得分:0)

以这种方式修改for循环: -

if let snapDict = snapshot.value as? NSMutableDictionary{
    for snap in snapDict{
       let user_ID = snap.key
       Base
     BASE_URL.child("meetups").child(user_ID).child("invitedFriends").observeEventType(.Value, withBlock: { snapshot in
     if snapshot.exists(){
         for invitees in snapshot.Value{
            let invitedFrnds = invitees.Value
            //Retrieving the friends.
            //Rest of your code accordingly
         }
      }
    })
  }
}
相关问题