Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Live Search on Titanium

I'm doing a live search where I execute queries as the user types.

I have a listener on my search field, and every time the user hits a key on the keyboard, I do a new search query.

searchField.addEventListener('change',function(e){
var rows = db.execute('...'); //Query
var data=[];
    //Loop through each row
    while (rows.isValidRow()) {                    

        //Create row                
           var row = Titanium.UI.createTableViewRow({ 
                      className: 'searchResultsRow'
                   });
            //Create row labels
            var rowTitle =  Titanium.UI.createLabel({ 
                text:rows.fieldByName('LocationID'), 
                //text style                
            });
            var rowSubTitle = Titanium.UI.createLabel({
                text:rows.fieldByName('Name'),
                //text style
            });                
            row.add(rowTitle);
            row.add(rowSubtitle);                        
            row.className = 'searchResultsRow';                
            data.push(row);
            searchResults.setData(data);                    
        rows.next();        
    }
    rows.close();
});

This is all fine and good, except that the search is pretty slow. I know this is because if the user types 4 letters really quickly, my app does a query on 1 letter, another query on 2 letters, etc. until finally doing that last query.

How do I cut a prior query short and always be querying on the latest text change?

— asked August 23rd 2010 by Clifton Labrum
  • database
  • query
  • search
1 Comment
  • Thanks for posting this. Very useful

    — commented October 3rd 2011 by Leonardo Amigoni

2 Answers

  • possible solution ( just brainstorming here ):

    maybe on change, detect how many characters are in the searchField ( the length of the text inside of it ). then create a setTimeout 1/2 second later or however many milliseconds later, and then in that setTimeout compare what the length of the characters are now compared to what they were BEFORE the setTimeout. if they are the same, then run your query. if they aren't, don't. that can help compensate for someone typing fast.

    rocksteady,
    danno~

    — answered August 23rd 2010 by danno watts
    permalink
    3 Comments
    • I think you're on to something. Good idea, thanks! I'll see what I can come up with and post the answer here.

      — commented August 23rd 2010 by Clifton Labrum
    • sounds good!

      rocksteady,
      danno~

      — commented August 25th 2010 by danno watts
    • I would just use a setTimeout() …

      — commented March 22nd 2011 by Michael Peng
  • Hi Clifton, were you able to get this live search to work?

    I am trying to do the same thing, however, I am newbie, not sure how I can query against a sqlite database. Do you have a working sample code you can share?

    thanks in advance.

    — answered August 6th 2011 by Jerry Lee
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.