您认为正在发生的事情并非如此。让我解释一下并提供一个例子:
您似乎正在尝试执行字符串搜索。甚至可能是子字符串搜索。
Firebase 不提供子字符串搜索功能,甚至字符串搜索也不完全是字符串搜索,因为它会像 swift 这样的语言。
所以对于初学者来说这是无效的
queryStarting(atValue: "[a-zA-Z0-9]*")
将逐字搜索以等于 [a-zA-Z0-9]* 的字符串或字符开头的节点。因此,如果您的节点恰好如下所示:
posts
post_x
description: "[a-zA-Z0-9]* This node would be returned"
这将是一场比赛。
.startWith: a query that starts with the given string
.endWith: a query ending with a string that starts with the given string
(not the ending part of a string or a substring)
让我根据你的结构提供一个示例结构
posts
post_1
description: "This is a post! #thisdoesntwork"
post_2
description: "Heres another post! #neitherdoesthis"
post_3
description: "a"
post_4
description: "n"
还有一个示例查询
let postsRef = ref.child("posts")
let queryRef = postsRef.queryOrdered(byChild: "description")
.queryStarting(atValue: "This")
.queryEnding(atValue: "z")
queryRef.observeSingleEvent(of: .value) { snapshot in
print(snapshot)
}
此查询将返回帖子 1、3 和 4。为什么?
post_1 以大写字母 T 开头,即 ascii 84。
查询将返回所有具有以 84 (ascii T) 开头并以 122 (ascii z) 结尾的 ascii 值的节点。所以post 3是a,即ascii 97,post 4,an n,是ascii 110。所以所有这些都返回了。
*对于后面的那些,查询实际上以单词“This”开头并以单词“z”结尾,但在此示例中进行了简化。
虽然一方面可能看起来有点限制,但它实际上非常强大。
一种用途是当您想要查询以特定字符串开头的一系列值时。因此,假设您拥有一家农产品分销公司,并拥有 Apple、Banana、Peanut 和 Walnut 等产品。你可以像这样组织你的数据库
items
item_0
description: fruit_apple
item_1
description: fruit_banana
item_2
description: nut_peanut
item_3
description: nut_walnut
如果你想要一份所有水果的清单,你可以这样查询
let queryRef = postsRef.queryOrdered(byChild: "description")
.queryStarting(atValue: "fruit_")
.queryEnding(atValue: "fruit_")
这称为复合值。
在您的情况下,最重要的答案是您不能直接在字符串中搜索特殊字符,但是,您可以搜索以 ascii 代码范围内的字符开头的字符范围。 p>
从“!”开始的查询并以“/”结尾将返回所有以字符开头的字符串:
33 !
34 \"
35 #
36 $
37 %
38 &
39 '
40 (
41 )
42 *
43 +
44 ,
45 -
46 .
47 /
这个超长的答案并不是真正的解决方案,但可能有助于重组您的 Firebase,以便您可以获取要查询的数据。