ios - Slow UISearchDisplayController with Core Data -
i have tableviewcontroller displaying 40 000 rows core data nsfetchedresultscontroller.
i implemented live search uisearchdisplaycontroller (support ios 7). it's working typing on keyboard when searching slow...
i'd appreciate if point me right direction , show me might going wrong.
here uisearchresultsupdating part in tableviewcontroller
- (bool)searchdisplaycontroller:(uisearchdisplaycontroller *)controller shouldreloadtableforsearchstring:(nsstring *)searchstring { itemsearchscope scopekey = controller.searchbar.selectedscopebuttonindex; [self searchfortext:searchstring scope:scopekey]; return yes; } - (bool)searchdisplaycontroller:(uisearchdisplaycontroller *)controller shouldreloadtableforsearchscope:(nsinteger)searchoption { nsstring *searchstring = controller.searchbar.text; [self searchfortext:searchstring scope:searchoption]; return yes; } - (void)searchfortext:(nsstring *)searchtext scope:(itemsearchscope)scopeoption { if (self.managedobjectcontext) { nsstring *predicateformat = @"%k contains[cd] %@"; nsstring *searchattribute1 = @"attribute1"; nsstring *searchattribute2 = @"attribute2"; nsstring *searchattribute3 = @"attribute3"; if (scopeoption == searchscopedebut) { predicateformat = @"%k beginswith[cd] %@"; } if (scopeoption == searchscopefin) { predicateformat = @"%k endswith[cd] %@"; } nspredicate *p1 = [nspredicate predicatewithformat:predicateformat, searchattribute1, searchtext]; nspredicate *p2 = [nspredicate predicatewithformat:predicateformat, searchattribute2, searchtext]; nspredicate *p3 = [nspredicate predicatewithformat:predicateformat, searchattribute3, searchtext]; nspredicate *predicate = [nscompoundpredicate orpredicatewithsubpredicates:@[p1, p2, p3]]; [self.searchfetchrequest setpredicate:predicate]; nserror *error = nil; self.filteredlist = [self.managedobjectcontext executefetchrequest:self.searchfetchrequest error:&error]; if (error) { nslog(@"searchfetchrequest failed: %@",[error localizeddescription]); } } }
i ended using nstimer delaying shouldreloadtableforsearchstring method. searchtimerpopped selector triggered if user stop typing keyboard 2 seconds.
@property (nonatomic, retain) nstimer *searchtimer; - (bool)searchdisplaycontroller:(uisearchdisplaycontroller *)controller shouldreloadtableforsearchstring:(nsstring *)searchstring { if (self.searchtimer) { [self.searchtimer invalidate]; self.searchtimer = nil; } self.searchtimer = [nstimer scheduledtimerwithtimeinterval:2 target:self selector:@selector(searchtimerpopped:) userinfo:searchstring repeats:false]; return no; } - (void)searchtimerpopped:(nstimer *)timer { nsstring *searchstring = [timer userinfo]; itemsearchscope scopekey = self.searchdisplaycontroller.searchbar.selectedscopebuttonindex; [self searchfortext:searchstring scope:scopekey]; [self.searchdisplaycontroller.searchresultstableview reloaddata]; }
Comments
Post a Comment