【问题标题】:Filtered searches with Firebase?使用 Firebase 过滤搜索?
【发布时间】:2016-09-05 17:18:59
【问题描述】:

我希望有一个搜索栏,用户可以在其中搜索 Firebase 上的用户,并在他们键入时更新表格视图,而不仅仅是搜索完全匹配。

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    if searchBar.text == nil || searchBar.text == "" {
        inSearchMode = false
        tableView.reloadData()  
    } else {
        inSearchMode = true
        let lower = searchBar.text!.lowercaseString

        filteredPeopleArray = peopleArray.filter({$0.title.hasPrefix(lower) != nil})
        tableView.reloadData()
}

此代码适用于此,但这需要我在加载时使用当前在 firebase 上的每个用户填充一个数组,并在应用打开时不断添加新用户。

我看到解析曾经有一个“查找密钥包含字符串的用户”,您会在 textDidChange 中进行查询以解析,但对于 Firebase,我没有看到类似的内容。另外,每次输入一个字母时,以任何方式向 Firebase 发送查询不会对数据征税吗?

Instagram 使用什么方法,例如,您输入一些字母并从服务器返回用户列表,并在其名称中使用该前缀?所以像 "Bo" 可能会返回一个 bob 和 bobbys 的列表。

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

    我无法发表评论,但这篇文章对我有帮助。

    https://www.quora.com/How-would-you-search-a-Firebase-Realtime-Database-with-a-substring

    假设您有以下 Firebase 数据

    {
      "lambeosaurus": {
        "dimensions": {
          "height" : 2.1,
          "length" : 12.5,
          "weight": 5000
        }
      },
      "stegosaurus": {
        "dimensions": {
          "height" : 4,
          "length" : 9,
          "weight" : 2500
        }
      }
    }
    

    以下示例查找名称以字母“b”开头的所有恐龙。

    var ref = new Firebase("https://example.firebaseio.com/");
    ref.orderByKey().startAt("b").endAt("b\uf8ff").on("child_added", function(snapshot) {
      console.log(snapshot.key());
    });
    

    【讨论】:

    • 那么我会在我的文本字段的“textDidChange”或其他任何内容中进行该查询吗?我担心的是,在每次击键时都向 firebase 发出请求会很残酷。或者这只是应用程序在这样的搜索时所做的事情?
    • 发布链接时,不要只提及链接。还包括它包含的内容,并在答案本身中提供代码。
    • 我本来只是将链接作为评论发布,但当时我没有代表这样做,但感谢您让我知道@Adarsh
    • @J.Doe 不一定要在每次击键时都出现,您只需有一个搜索按钮,当按下时,搜索当前输入的字符串。
    猜你喜欢
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 2013-05-24
    • 1970-01-01
    相关资源
    最近更新 更多