Swift - 数据库中的 Firebase 搜索

标签 swift firebase firebase-realtime-database

我一边打字一边搜索用户。但是最后它返回每个用户。 我不知道为什么会这样。好像还行

 class func findUsers(text: String)->[User]{
    let user = User()
    var users = [User]()
    ref.child("Users").queryOrderedByChild("name").queryStartingAtValue(text).observeEventType(.Value, withBlock: { snapshot in
        for u in snapshot.children{
            user.name = u.value!["name"] as? String
            user.surname = u.value!["surname"] as? String
            user.email = u.value!["email"] as? String
            user.score = u.value!["score"] as? Int
            user.status = u.value!["status"] as? Int
            user.id = u.value!["id"] as? String
            user.pin = u.value!["pin"] as? String
            users.append(user)
            print(user.name)
        }
    })
    print("users:", users)
    return users
}

最佳答案

您正在使用 queryStartingAtValue。来自documentation :

The FIRDatabaseQuery instance returned by queryStartingAtValue:childKey will respond to events at nodes with a value greater than startValue, or equal to startValue and with a key greater than or equal to childKey.

因此请记住,如果您搜索用户的 name,它会给出名称按字母顺序排列大于给定用户的所有用户。您需要设置一个queryEndingAtValue来限制查询范围。

ref.child("Users").queryOrderedByChild("name").queryStartingAtValue(text).queryEndingAtValue(text+"\u{f8ff}")

Firebase legacy documentation是了解 firebase 如何处理查询的好资源。在那里你会发现:

The f8ff character used in the query above is a very high code point in the Unicode range. Because it is after most regular characters in Unicode, the query matches all values that start with a b.

您可能还会对 Elasticsearch 感兴趣. Flashlight是一个集成elasticsearch和firebase的节点服务。


更新:

正如评论中所指出的那样,如果您要搜索的字符串包含大写字符,则此方法将不起作用。在这种情况下,解决方案是在对象中有一个附加字段,其中包含您正在搜索的所有小写信息。因此,如果您将 Satoshi Nakamoto 作为 name,您将使用 satoshi nakamoto 保存一个额外的 nameSearch 属性。

最后确保您的搜索字符串也是小写的。


您的代码还有其他一些问题。

  • 您正在设置观察者。它会在您的数据更改时随时触发回调,因此您应该在回调内重置 users 数组,这样您就不会拥有重复和脏数据。

  • 您的函数不会返回用户数据,因为 firebase 调用是异步的。当您调用 return users 时,您不会填充 users 数组。我建议您使用 self.users = users 而不是在函数中返回它。否则,如果你想继续当前的方法并返回它,你应该设置一个 completion handler .


class func findUsers(text: String)->Void{
    ref.child("Users").queryOrderedByChild("name").queryStartingAtValue(text).queryEndingAtValue(text+"\u{f8ff}").observeEventType(.Value, withBlock: { snapshot in
        var user = User()
        var users = [User]()
        for u in snapshot.children{
            user.name = u.value!["name"] as? String
            ...
            users.append(user)
        }
        self.users = users
    })
}

关于Swift - 数据库中的 Firebase 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38274568/

相关文章:

ios - 避免 Firebase 中出现重复条目

swift - JSQMessagesViewController 设置收件人 firebase id

swift - 如何将这种副作用转化为优雅的 Vapor 操作链?

ios - WKWebView 将谷歌搜索结果显示为原始 html

swift - HTTP POST 请求返回尝试从对象[0] 插入 nil 对象?

swift - 如何自定义 tintColor 和调整 UITabBarItem 的大小

ios - FCM 通知未收到 iOS

java - 是否可以通过POSTMAN向特定应用程序包名称发送FCM推送通知?

firebase - 任何人都可以在dart中优化此代码。我正在为每个单词制作搜索过滤器

ios - Firebase - iO : How to pass data from Tableview to DetailsView?