How to get all child data from firebase without knowing the path

谁说我不能喝 提交于 2019-12-25 00:27:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!