text search too slow on sqlite db on tableview on iphone

前端 未结 2 410
盖世英雄少女心
盖世英雄少女心 2021-01-03 16:37

I have a large table of around 2500 entries. I am displaying it on tableview. however the search bar is too slow while doing dynamic search. ie I am filtering the table ever

2条回答
  •  孤街浪徒
    2021-01-03 17:29

    After the first character, copy the resulting data into an array and then filter the array using a predicate. It's much faster. Then reload your table from the array. The array might be an array of dictionaries where each element is a dictionary with the search string (i.e. name or whatever) and the other entry is a reference to the core data entity that you want as the final selection. Or you can just so the core data selection at the end when the user makes a selection.

    Note that when you filter with a predicate, the objects in the array need to have properties that match what you are searching for. In my example, I created an object that had properties fullName emailAddressString etc..

    predicate = [NSPredicate predicateWithFormat:@"fullName contains[cd] %@ or emailAddressString contains[cd] %@", searchString, searchString];

    NSArray *resultArray = [[NSArray alloc] initWithArray:[allContacts filteredArrayUsingPredicate:predicate]];

提交回复
热议问题