首页 文章

使用Firebase过滤搜索?

提问于
浏览
1

我希望有一个搜索栏,用户可以在Firebase上搜索用户,并在键入时更新tableview,而不只是搜索完全匹配 .

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列表 .

1 回答

  • 4

    我无法评论,但这是一篇帮助我的文章 .

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

    假设您有以下火力数据

    {
      "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());
    });
    

相关问题