Firebase实时数据库:如何侦听节点项的特定子集? [英] Firebase realtime database: How to listen to a specific subset of items of a node?
问题描述
我的firebase实时数据库中具有以下结构:
I have the following structure within my firebase realtime database:
- 艺术家
- ArtistID
- 名称
- listOfPaintings
- PaintingID1
- PaintingID2
- artists
- ArtistID
- name
- listOfPaintings
- PaintingID1
- PaintingID2
- PaintingID1
- 副本
- 价格
- PaintingID1
- copies
- price
- 副本
- 价格
- 副本
- 价格
现在,我想听听特定的paintingID和特定的ArtistID的更改. 例如,我想听PaintingID1,PaintingID6,PaintingID7,PaintingID11.
Now I want to listen for changes of specific paintingIDs and specific ArtistIDs. For example I want to listen to PaintingID1, PaintingID6, PaintingID7, PaintingID11.
到目前为止我的存储库代码:
My repository code so far:
override fun observeThisPainting(painting: Painting, receiveDataFromBackendCallback: DataFromBackendCallback<Painting>) { this.receiveDataFromBackendCallback = receiveDataFromBackendCallback val myRef = database.child("paintings").child(painting.paintingId) myRef.addValueEventListener(this) } override fun onDataChange(snapshot: DataSnapshot) { val painting = snapshot.getValue(Painting::class.java) painting?.let { receiveDataFromBackendCallback.onSuccess(it) } } override fun onCancelled(error: DatabaseError) { }
- 我的RTDB的架构正确吗?在我的第一次尝试中,我将所有画都保存在艺术家listOfPaintings中.现在,我只为他们保存密钥.
- 如何实现从视图模型中侦听特定节点的对象的子集? (我知道如何听绘画节点的所有项目,但我想知道是否有办法听这个节点的特殊项目)
感谢您的想法!
推荐答案
似乎您是在单个根节点下嵌套多种不同类型的数据,Firebase文档位于
It seems that you're nesting multiple different types of data under a single root node, which the Firebase documentation on structuring data recommends against. I'd usually expect three top-level nodes, in a JSON like this:
Artists: { "ArtistId1": { name: "...", otherdata... }, "ArtistId2": { name: "...", otherdata... } }, ArtistPaintings: { "ArtistId1": { "Painting1": true, "Painting3": true }, "ArtistId2": { "Painting2": true, "Painting4": true } }, Paintings: { "Painting1": { ... }, "Painting2": { ... }, "Painting3": { ... }, "Painting4": { ... }, }
使用上述内容,您可以显示所有艺术家姓名的列表,而不必下载所有艺术家的所有绘画ID.
With the above you can show a list of all artist names, without also having to download all he painting IDs of all artists.
然后,当用户选择一个艺术家时,您可以加载绘画ID,然后加载该艺术家的绘画.如果有帮助,您可以存储比
true
更有意义的内容,例如绘画的标题或其元数据的子集,以便无需进一步加载即可显示绘画列表.Then when the user selects an artist, you can load the painting IDs and then the paintings for that artist. If helpful, you can store something more meaningful than the
true
, like the painting's title, or a subset of its metadata, so that you can show a list of paintings without further loads.如果您可能还需要查找艺术家来绘画,通常会添加第四个顶级节点
PaintingArtists
.And if you have a situation where you might also need to look up the artist(s) for a painting, you'll typically add a fourth top-level node
PaintingArtists
.有关此内容的更多信息,请参见我对> Firebase中多对多关系.
For more on this see my answer to Many to Many relationship in Firebase.
关于第二个问题(请不要在以后的单个帖子中放置多个问题):侦听节点子集的唯一方法是通过查询.因此,例如,如果您想收听某个价格的所有绘画,则可以查询价格.
On your second question (please don't put multiple questions in a single post in the future): the only way to listen to a subset of nodes is through a query. So for example if you want to listen to all paintings of a certain price, you'd perform a query on the price.
如果您想听某位艺术家的所有画作,则可以选择:
If you want to listen to all paintings of a certain artist, your options are to:
- 在每幅画中包括艺术家ID,并使用查询.在使用NoSQL数据库时,这种数据重复非常普遍,因为在应用程序中实现其他用例时,您常常最终会修改数据模型.
- 从
/ArtistPaintings
获取艺术家的绘画ID,然后为每个绘画使用单独的侦听器.正如Firebase
- Include the Artist ID in each painting, and use a query. This sort of data duplication is very common when using NoSQL databases, as you often end up modifying your data model as you implement additional use-cases in your app.
- Reach the painting IDs for the artist from
/ArtistPaintings
and then use a separate listener for each painting. This is not nearly as slow/resource intensive as you may think, as Firebase pipelines the requests over a single socket.
要了解有关一般NoSQL数据建模(尤其是Firebase)的更多信息,请阅读 NoSQL数据建模,请观看 Firebase for SQL开发人员,以及了解Cloud Firestore (后者用于Cloud Firestore,但其中很多机制也适用于实时数据库.
To learn more about NoSQL data modeling in general and Firebase in particular, read NoSQL data modeling, watch Firebase for SQL developers, and possibly Getting to know Cloud Firestore (the latter is for Cloud Firestore, but many of the mechanisms apply to Realtime Database too).
这篇关于Firebase实时数据库:如何侦听节点项的特定子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- ArtistID
- ArtistID