如何在不知道路径的情况下从Firebase获取所有子数据 [英] How to get all child data from firebase without knowing the path
问题描述
我的数据结构如下:
我希望能够访问每个用户列表,使所有列表组成一个数组,但是我不知道每个用户的ID或列表ID,所以我不能这样做:
Database.database().reference.child("users").child(uid)......
,因为它不仅用于一个UID.从火力山中拉出时,是否有办法跳过" .child(uid)
和.child(listingID)
?
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?
推荐答案
如果要检索所有用户的所有数据,则可以将观察者附加到整个/users
节点.这将为您提供完成处理程序中所有数据的快照,然后您可以遍历该快照的子级以获取每个单独的用户.
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.
类似这样的东西:
ref.observeSingleEvent(of: .value) { snapshot in
for case let user as FIRDataSnapshot in snapshot.children {
print(user.childSnapshot(forPath: "email").value)
}
}
您可以使用类似的循环进一步深入了解快照的数据,并更多地调用childSnapshot(forPath
:以查找特定属性.
You can use similar loops to dive further down into the snapshot's data, and more calls to childSnapshot(forPath
: to find specific properties.
也可以看到我一个小时前给出的答案如何在同一循环中一次循环所有Firebase子级?和循环输入Firebase
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
这篇关于如何在不知道路径的情况下从Firebase获取所有子数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!