鉴于以下结构,我该如何检索特定用户通知?
ie:uid0001
应该能够检索列表:[xxxxx01, xxxxx02]
:
notifications
uid01
xxxxx01
uid02
xxxxx02
uid03
xxxxx03
uid04
xxxxx04
users
uid0001
notifications
uid01
uid02
不幸的是,这种怪异效率非常低:
firebase.database().ref(`users/${user.userId}/notifications`).once('value').then((snapshot) => {
snapshot.forEach((data) => {
firebase.database().ref(`notifications/${data.key}`).once('value').then((snap) => {
this.list.unshift(localUpdates)
})
})
})
可能是某些事情(警告,伪代码即将来临):
firebase.database().ref('notifications').on('child_added').equalTo('whatever the childs key is here').then((snapshot) => {
})
总是任何方向,欢迎嘲笑和腐烂的水果,谢谢!
答案 0 :(得分:1)
有许多解决方案;这是两个(原谅伪代码,因为我不知道你的平台)
好的方式
使用现有结构,捕获/ users / uid0001 / notifications中的子节点,然后迭代这些子节点,一次一个地读取通知节点中的子节点。
更好的方式
notifications
-yisijaoijsijdasd //generated by push() or childByAutoId()
for_user: "uid01"
notification: "Check this out"
timestamp: "20170421"
user_time: "uid01_20170421" //concatenated allows queries by > 1 item
-Y9s90kas9dkka9s
for_user: uid01
notification: "Yipee"
timestamp: "20170426"
user_time: "uid01_20170426"
users
uid0001
然后查询通知节点:
uid01的所有通知
notificationsRef.queryOrdered(byChild: "for_user").equalTo("uid01")
过去4天内uid01的所有通知
notificationsRef.queryOrdered(byChild: "user_time")
.startingAt("uid01_20170422")
.endingAt("uid01_20170426")
你可以对此进行扩展 - 例如,如果你想跟踪尚未阅读的通知,你可以添加
notifications
-yisijaoijsijdasd //generated by push() or childByAutoId()
for_user: "uid01"
notification: "Check this out"
timestamp: "20170421"
user_time: "uid01_20170421"
was_read: false
user_read: "uid01_false"
并查询
notificationsRef.queryOrdered(byChild: "user_read").equalTo("uid01_false")