1

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!

chickenparm
  • 1,570
  • 1
  • 16
  • 36

1 Answers1

0

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
         }
      }
    })
  }
}
Dravidian
  • 9,945
  • 3
  • 34
  • 74