如何在Swift中使用Firebase实时数据库进行’like’查询风格的读取

huangapple go评论76阅读模式
英文:

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:

如何在Swift中使用Firebase实时数据库进行’like’查询风格的读取

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.

huangapple
  • 本文由 发表于 2023年6月8日 07:34:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定