英文:
How to read from a Firebase Realtime DB with a 'like' query style in Swift
问题
如何从以下 Firebase 节点设置中获取所有子项 0yV3uJZAoDWCZNHuRgxrUlKYFgz1
的方法有人可以告诉我吗:
我有以下代码:
self.groupRef
.child(groupId)
.queryOrderedByKey()
.queryStarting(afterValue: userId)
.queryEnding(beforeValue: userId)
.observeSingleEvent(of: .value, with: { snap in
})
我想要获取作为用户 ID 一部分的 -1
和 -2
。
英文:
Can someone tell me how I can get the ALL the children 0yV3uJZAoDWCZNHuRgxrUlKYFgz1
from the following firebase node setup:
I have the following code:
self.groupRef
.child(groupId)
.queryOrderedByKey()
.queryStarting(afterValue: userId)
.queryEnding(beforeValue: userId)
.observeSingleEvent(of: .value, with: { snap in
})
I would like to get the -1
and -2
as part of the user id.
答案1
得分: 0
你的查询目前从 userId
开始并以此结束,这意味着你只获取一个节点。
如果你想获取以 userId
值开头的所有节点,你需要结束在下一个节点。通常的方法是:
self.groupRef
.child(groupId)
.queryOrderedByKey()
.queryStarting(afterValue: userId)
.queryEnding(beforeValue: userId+"~") // 🧀
.observeSingleEvent(of: .value, with: { snap in
})
这里的 ~
并不是一个魔术运算符,而只是ASCII表中的最后一个字符之一。
英文:
Your query currently starts and ends at userId
, which means that you get only one node.
If you want to have all nodes that start with the value of userId
, you'll want to end at the next node. The common trick for this is:
self.groupRef
.child(groupId)
.queryOrderedByKey()
.queryStarting(afterValue: userId)
.queryEnding(beforeValue: userId+"~") // 👈
.observeSingleEvent(of: .value, with: { snap in
})
The ~
here is not a magic operator, but just one of the last characters in the ASCII table.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论