4

I have added UISearchBar and UITableView in my project. I need to filter all the users according to the text in UISearchBar and display in the tableview. I do research for the query in firebase documentation but I did not find. I just need the firebase query for partial string. Can any one help me?

sant05
  • 418
  • 7
  • 21
  • Are you performing a partial string search? e.g. as the user is typing in the UISearchBar you want to autofill from a list that updates the list with fewer and fewer matches as the user types? Or do they type the entire string and search for that entire string. – Jay Jun 17 '16 at 15:16
  • hey @Jay thank you for the reply. Actually I am performing partial string search. – sant05 Jun 19 '16 at 03:25
  • 1
    Firebase doesn't offer a way to do a partial string search (a like or contains type query). The caveat to that is if you are searching the first part of the string - there's a technique to do that using .startingAt and .endingAt with a Unicode char, \uf8ff. Also, you *can* set up a Firebase structure to search for a partial string. [See Autocomplete with Firebase](http://stackoverflow.com/questions/23506800/autocomplete-with-firebase/23510916#23510916) and [Searching in Firebase](http://stackoverflow.com/questions/33867185/searching-in-firebase-without-server-side-code) – Jay Jun 19 '16 at 13:02
  • I solved my problem by using the technique exactly what you suggest. I re structure the database according to the edward suggestion. Thanks for your help, I really appreciate it. – sant05 Jun 20 '16 at 09:21

3 Answers3

4

I believe you have these in your firebase database

  • firstName
  • lastName

and firebase won't let you do multiple where clause, to solve this, just add a new field, so it will become like this

  • firstName
  • lastName
  • fullName


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

   }
Edward Anthony
  • 3,354
  • 3
  • 25
  • 40
  • @EdwardAnthony How will this allow the OP to do a partial string search as stated in the question? – Jay Jun 19 '16 at 13:04
  • @Jay If it's partial, you can just make the index yourself. For example, a field `first1Charater`, `first2Charater`, `first3Charater`, and so on. And you query it by using `queryOrderedByChild("first\(input.characters.count)Character")` – Edward Anthony Jun 19 '16 at 20:34
0

Did you look at this page ?

I think QueryToValue can be useful here.

Bram'in
  • 618
  • 1
  • 7
  • 18
-2

hello hope this helps you.. if this ans works then please make it correct so that other user can identify answer very quickly....

i hope i understand your question.....

@IBOutlet var searchBar: UISearchBar!

@IBOutlet var tblview: UITableView!

step 1:-make arary

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

step2:- declare this variable global just above the viewDidLaod() method

 var filteredData: [String]!

step3:-

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

}

step 4:-use Tableview delegate methods

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
}

step 5-use this below method..

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 Raghani
  • 557
  • 1
  • 9
  • 21
  • 1
    Thanks @akash for your answer.This filter from the in memory data. But i need to directly filter from firebase database. – sant05 Jun 17 '16 at 11:18