简体   繁体   English

Sqlite.swift实时搜索

[英]Sqlite.swift live search

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. 可以说我想按公司搜索,并且在名为companys的列上有以下信息。

  • Facebook Facebook的
  • FastCompany FastCompany
  • Facebook Facebook的
  • Google 谷歌
  • Microsoft 微软

I have a textfield that has calls a function on editchanged. 我有一个在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 ) 如果我开始输入“ fa ”,它将显示( Facebook,Facebook和FastCompany

If I continue typing " fac " it will show ( Facebook, Facebook ) 如果我继续输入“ fac ”,它将显示( Facebook,Facebook

But when I delete the last character " c " from the searchbox (leaving it in "fa" again) then the query displays nothing. 但是,当我从搜索框中删除最后一个字符“ c ”(再次将其保留在“ fa”中)时,查询将不显示任何内容。

Any ideas on how I can solve this. 关于如何解决此问题的任何想法。

I think your issue is coming from over writing the user object with each search. 我认为您的问题是由于每次搜索都覆盖了user对象。 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语句。

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

Then use the currentQuery object to display the results instead. 然后,使用currentQuery对象显示结果。 This way, no matter what you're searching, it will match everything. 这样,无论您要搜索什么,它都可以匹配所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM