查询Firebase嵌套的子Swift [英] Query Firebase for nested child swift

查看:116
本文介绍了查询Firebase嵌套的子Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以下是我的数据在Firebase中的结构:

   -  Meetups 
- UniqueID
- 地址:xxxx
- 日期:xxxx
- 创建者:xxxx
- inviteFriends
- 1:xxxx
- 2:xxxx
- 3:xxxx

我试图找出如何检索inviteFriends数据。

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

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

self.meetups = []


如果让snapshots = snapshot.children.allObjects as?[FIRDataSnapshot] {

快照中的快照{

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

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

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

}
}

这使我可以从地址,日期和创建者中创建我的Meetup类的实例。不过,我最近将inviteFriends数据添加到了结构中,并且试图找出如何在同一个进程中查询它。感谢您的任何建议!

解决方案

通过这种方式修改您的 for : -

 如果让snapDict = snapshot.value为? NSMutableDictionary {
for snapDict(
let user_ID = snap.key
Base
BASE_URL.child(meetups)。child(user_ID).child(invitedFriends) .observeEventType(.Value,withBlock:{snapshot in $ b $ if if snapshot.exists(){
for snapshotsValue中的被邀请者{
let inviteFrnds = invitees.Value
//检索朋友。
//其余的代码
}
}
})
}
}


I need help getting nested data in Firebase.

Here is how my data is structured in Firebase:

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

I am trying to figure out how to retrieve the invitedFriends data.

Currently I retrieve the data this way:

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)

      }
    }

This allows me to create an instance of my Meetup class from the address, date, and creator. However, I recently added the invitedFriends data to the structure and I am trying to figure out how to query it in this same process. Thank you for any suggestions!

解决方案

Modify your for loop this way:-

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

这篇关于查询Firebase嵌套的子Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆