代码之家  ›  专栏  ›  技术社区  ›  Learn2Code

如何在Swift中使用“like”查询样式读取Firebase实时数据库

  •  0
  • Learn2Code  · 技术社区  · 2 年前

    有人能告诉我怎样才能得到所有的孩子吗 0yV3uJZAoDWCZNHuRgxrUlKYFgz1 从以下firebase节点设置:

    enter image description here

    我有以下代码:

    self.groupRef
      .child(groupId)
      .queryOrderedByKey()
      .queryStarting(afterValue: userId)
      .queryEnding(beforeValue: userId)
      .observeSingleEvent(of: .value, with: { snap in
                   
      })
    

    我想得到 -1 -2 作为用户id的一部分。

    1 回复  |  直到 2 年前
        1
  •  0
  •   Frank van Puffelen    2 年前

    您的查询当前开始和结束于 userId ,这意味着您只得到一个节点。

    如果要启动所有节点 具有 的价值 userId ,您将希望在下一个节点结束。常见的技巧是:

    self.groupRef
      .child(groupId)
      .queryOrderedByKey()
      .queryStarting(afterValue: userId)
      .queryEnding(beforeValue: userId+"~") // 👈
      .observeSingleEvent(of: .value, with: { snap in
                   
      })
    

    这个 ~ 这里不是一个魔术运算符,而是ASCII表中最后一个字符。