问题
My data structure looks like this:
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?
回答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
来源:https://stackoverflow.com/questions/56640423/how-to-get-all-child-data-from-firebase-without-knowing-the-path