简体   繁体   中英

How to retrieve the data from Firebase Database inside muti-layer multiple child nodes?

我的数据库快照

I have been trying to retrieve the data from Firebase Database but unable to find a way with this type of data structure. As you can see in the AllStores node, there are multiple stores and each store has its own data with own products and the products have many main categories and each main category has multiple sub category , sub- categories have multiple brands and finally each brand has multiple products with a random number as its node. I know this structure is a bit complicated and i can get the data by separating the nodes and create again and again which is a bit tedious. So, is the any possible way of retrieving the whole store with all of its data in an single object and put it on a list or do i have to manually put the path by just retrieving the store data and use reference.child("") method to get each and every data. If i find a way of retrieving this data structure without changing its nodes , it will be of great ease to me . Any answer is much appreciated.

is the any possible way of retrieving the whole store with all of its data in an single object

Yes, this is already what happens when you fetch a node from Realtime Database. When you get DataSnapshot for any given node, it will contain all of the child nodes inside it. All you have to do is use child() on that snapshot to dig into each of the child nodes to get a new DataSnapshot with all of the child data. Keep digging in to each child the same way. Everything child will be nested inside other DataSnapshots.

So, if you have a DataSnapshot of "/AllStores/Mazbat Store", you can go down as far as you need to find a snapshot with the data you want.

DataSnapshot store = // fetch of "/AllStores/Mazbat Store"
DataSnapshot item = store.child("Store Products").child("Electronics").child("Camera").child("Asus").("6432649")

You will obviously have to write a lot of code to get a hold of everything from the top level snapshot, but it will all be there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM