Firebase和在Swift中查询术语 [英] Firebase and querying for a term in Swift
问题描述
我正在为我的应用程序使用Firebase,并试图查询数据库的内容.我正在使用以下查询:
I am using Firebase for my app and am trying to query the contents of my database. I am using the following query:
DataService.dataService.BASE_REF.child("Posts").
child(selectedComment.commentKey).child("comments").
queryOrderedByChild("userComment").queryEqualToValue(comment).
observeSingleEventOfType(.Value, withBlock: { (snapshot) in
例如如果我要搜索"bose"一词,则只会显示bose,但会显示"Bose","BOSE"和""Bose XYZ"不会显示.我该如何查询,以便在查询"bose"时所有上述内容都显示出来
For eg. if I am searching for the term "bose", then only bose shows up, but "Bose", "BOSE" & "Bose XYZ" won't show up. How would I go about querying such that all of the above show up when I query "bose"
推荐答案
由于使用的是queryEqualToValue
,因此只有在userComment
与您指定的值完全匹配的情况下,才能获得结果.
Since you're using queryEqualToValue
, you will only get results where the userComment
matches exactly with the value you specified.
如果要以userComments
以值开头的结果,则应使用queryStartingAtValue
和queryEndingAtValue
的组合:
If you want results where userComments
starts with the value, you should use a combination of queryStartingAtValue
and queryEndingAtValue
:
DataService.dataService.BASE_REF.child("Posts").
child(selectedComment.commentKey).child("comments").
queryOrderedByChild("userComment")
.queryStartingAtValue(comment). queryEndingAtValue(comment+"\uF8FF")
observeSingleEventOfType(.Value, withBlock: { (snapshot) in
这提供了一系列结果,从以comment
开头到以comment
开头,后跟最后一个Unicode字符的结果.
This gives you a range of results, from starting with comment
to those starting with comment
followed by the last unicode character.
但这仍然不匹配大写和小写结果.如果需要,您必须将数据存储在统一的大小写中才能进行搜索. Firebase数据库更注重实时数据同步,而不是其(文本)搜索功能的完整性.
But this still won't match uppercase and lowercase results. If you want that, you will have to store the data in a uniform case to enable searching. The Firebase Database is more focused on realtime data synchronization than on completeness of its (text) search capabilities.
另请参阅:
- Case insensitive sorting with Firebase orderByChild
- firebase query methods startAt() taking case sensitive parameters
- how to filter android listview items with firebase query?
这篇关于Firebase和在Swift中查询术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!