0

My data structure looks like this:

Data structure

I want to be able to access every users listings to make an array of all listings however I do not know every users ID or Listing ID so i can not do: Database.database().reference.child("users").child(uid)...... because it is not for just one UID. if there a way to 'skip' over the .child(uid) and .child(listingID) when pulling from firebase?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Dane
  • 45
  • 6
  • Database.database().reference.observe(DataEventType.value) { (snapshot) in if snapshot.childrenCount > 0 { for items in snapshot.children.allObjects as! [DataSnapshot] { let object = items.value as! [String: AnyObject] let email = object["email"] as! String } } } – El Tomato Jun 18 '19 at 01:04

1 Answers1

1

If you want to retrieve all data for all users, you can attach your observer to the entire /users node. That will give you a snapshot of all data in the completion handler, and you can then loop over the children of that snapshot to get at each individual user.

Something like this:

ref.observeSingleEvent(of: .value) { snapshot in
    for case let user as FIRDataSnapshot in snapshot.children {
        print(user.childSnapshot(forPath: "email").value)
    }
}

You can use similar loops to dive further down into the snapshot's data, and more calls to childSnapshot(forPath: to find specific properties.

Also see the answer I gave just an hour ago here and How do I loop all Firebase children at once in the same loop? and Looping in Firebase

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807