【问题标题】:Search all user by user's full name in firebase IOS [closed]在firebase IOS中按用户全名搜索所有用户[关闭]
【发布时间】:2016-10-19 04:39:04
【问题描述】:

我在我的项目中添加了UISearchBar 和UITableView。我需要根据UISearchBar中的文字过滤所有用户并显示在tableview中。我对 firebase 文档中的查询进行了研究,但没有找到。我只需要部分字符串的firebase查询。谁能帮我?

【问题讨论】:

  • 您是否在执行部分字符串搜索?例如当用户在 UISearchBar 中键入时,您希望从列表中自动填充,该列表随着用户键入而更新列表的匹配项越来越少?还是他们键入整个字符串并搜索整个字符串。
  • 嘿@Jay 谢谢你的回复。实际上我正在执行部分字符串搜索。
  • Firebase 不提供进行部分字符串搜索(类似或包含类型查询)的方法。需要注意的是,如果您正在搜索字符串的第一部分 - 有一种技术可以使用带有 Unicode 字符 \uf8ff 的 .startingAt 和 .endingAt 来做到这一点。此外,您可以设置 Firebase 结构来搜索部分字符串。 See Autocomplete with Firebase 和 Searching in Firebase
  • 我通过使用您所建议的技术解决了我的问题。我根据爱德华的建议重新构建数据库。感谢您的帮助,我真的很感激。

标签: ios swift elasticsearch firebase firebase-realtime-database


【解决方案1】:

我相信你的 firebase 数据库中有这些

  • 名字
  • 姓氏

而且firebase不会让你做多个where子句,要解决这个问题,只需添加一个新字段,它就会变成这样

  • 名字
  • 姓氏
  • 全名


ref.child("users")
   .queryOrderedByChild("fullName")
   .queryEqualToValue("your full name")
   .observeSingleEventOfType(.Value, withBlock: { (snapshot) -> Void in

   }

【讨论】:

  • @EdwardAnthony 这将如何允许 OP 如问题中所述进行部分字符串搜索?
  • @Jay 如果它是部分的,你可以自己做索引。例如,first1Charater、first2Charater、first3Charater 等字段。然后使用queryOrderedByChild("first\(input.characters.count)Character") 查询它
【解决方案2】:

你看过这个page 吗?

我认为 QueryToValue 在这里很有用。

【讨论】:

    【解决方案3】:

    您好希望这对您有所帮助..如果此答案有效,请使其正确,以便其他用户可以很快识别答案....

    希望我能理解你的问题.....

    @IBOutlet var searchBar: UISearchBar!
    
    @IBOutlet var tblview: UITableView!
    

    第 1 步:-制作数组

    let data =
    ["xxx", "india, CA", "pakistan", "usa",
        "china", "fgsfsgs", "San Diego, CA", "San Antonio, TX",
        "Dallas, TX", "Detroit, MI"]
    

    step2:- 在 vi​​ewDidLaod() 方法上方声明这个变量为全局变量

     var filteredData: [String]!
    

    第三步:-

    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        tblview.dataSource = self
        searchBar.delegate = self
        filteredData = data
    
    }
    

    第 4 步:使用 Tableview 委托方法

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
    
        let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"cell")
        cell.textLabel?.text = filteredData[indexPath.row]
    
        return cell
    }
    
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredData.count
    }
    

    第 5 步-使用下面的方法..

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) 
    {
        // When there is no text, filteredData is the same as the original data
        if searchText.isEmpty {
            filteredData = data //use your array in place od data
        } else {
            // The user has entered text into the search box
            // Use the filter method to iterate over all items in the data array
            // For each item, return true if the item should be included and false if the
            // item should NOT be included
            filteredData = data.filter({(dataItem: String) -> Bool in
                // If dataItem matches the searchText, return true to include it
                if dataItem.rangeOfString(searchText, options: .CaseInsensitiveSearch) != nil {
                    return true
                } else {
                    return false
                }
            })
        }
        self.tblview.reloadData()
    }
    

    【讨论】:

    • 感谢@akash 的回答。此过滤器来自内存数据。但我需要直接从 firebase 数据库中过滤。
    猜你喜欢
    • 2013-09-23
    • 2019-03-23
    • 1970-01-01
    • 2021-11-24
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 2019-01-25
    相关资源
    最近更新 更多