1

I want to do a live search on the DB.

Lets say I want to search by companies and I have the following info on a column named companies.

  • Facebook
  • FastCompany
  • Facebook
  • Google
  • Microsoft

I have a textfield that has calls a function on editchanged.

   @IBAction func searching(sender: AnyObject) {
        tempstring = "%"+searchBar.text+"%"
        println(tempstring)

         user = user.select(name)
            .filter(like(tempstring, name))
            .limit(30, offset: 0)
        collectionView?.reloadData()
}

It kind of works, if I start typing "fa" It will show (Facebook, Facebook and FastCompany)

If I continue typing "fac" it will show (Facebook, Facebook)

But when I delete the last character "c" from the searchbox (leaving it in "fa" again) then the query displays nothing.

Any ideas on how I can solve this.

danielsalare
  • 335
  • 3
  • 14

1 Answers1

1

I think your issue is coming from over writing the user object with each search. This is fine as long as you only move forward, but when you go backwards like you did, the query messes up.

Instead, try adding a currentQuery property to your view controller with the collection view in it and set it to your user.select statement.

currentQuery = user.select(name)
        .filter(like(tempstring, name))
        .limit(30, offset: 0)

Then use the currentQuery object to display the results instead. This way, no matter what you're searching, it will match everything.

mbottone
  • 1,219
  • 9
  • 13