【问题标题】:Query Firebase for nested child swift为嵌套的子 swift 查询 Firebase
【发布时间】:2016-12-11 08:13:14
【问题描述】:

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

这是我的数据在 Firebase 中的结构:

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

我正试图弄清楚如何检索被邀请的朋友数据。

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

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 类的实例。但是,我最近将被邀请的朋友数据添加到结构中,并且我试图弄清楚如何在同一过程中查询它。感谢您的任何建议!

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    以这种方式修改您的 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
             }
          }
        })
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-01
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 2020-11-18
      相关资源
      最近更新 更多