performance - iOS - searching in a large NSArray is slow -
i'm building customised uitableviewcontroller shows contacts in iphone , behaves abpeoplepickernavigationcontroller. meaning supports searching contacts. i'm doing code here.
i've implemented search ability using search bar , search display controller
, , followed tutorial appcoda.
since nsarray array of abrecordref
method of filtercontentforsearchtext: scope:
this:
- (void)filtercontentforsearchtext:(nsstring*)searchtext scope:(nsstring*)scope { nspredicate *resultpredicate = [nspredicate predicatewithblock:^bool(id evaluatedobject, nsdictionary *bindings) { abrecordref person = (__bridge abrecordref)evaluatedobject; nsstring * fullname = [self getfullnameofrecord:person]; nspredicate *tmppredicate = [nspredicate predicatewithformat:@"self contains[c] %@", searchtext]; if ([tmppredicate evaluatewithobject:fullname]) { return yes; } else { nslog(@"tmppredicate didn't match"); return no; } }]; searchresults = [self.allcontacts filteredarrayusingpredicate:resultpredicate]; }
the search results fine, since large array, works slowly. there way improve performance of search mechanism?
update: @eiko suggested, tried replacing inner nspredicate code:
nsrange range = [fullname rangeofstring:searchtext options:nscaseinsensitivesearch]; if (range.length > 0) { return yes; } else { return no; }
but didn't improve performance.
you should try use profiler find weakest line, assume problem predicate block evaluated every entry every time.
i suggest create own wrapper class abrecordref (let's recordwrapper), contain link abrecordref whole data , cache frequent used , important values (e.g. fullname), can obtain once while loading list of contacts.
then if have array of recordwrapper* objects can filter calling
nspredicate *resultpredicate = [nspredicate predicatewithformat:@"fullname contains[c] %@", searchtext]; searchresults = [self.allcontactswrappers filteredarrayusingpredicate:resultpredicate];
this should increase filtering speed.
Comments
Post a Comment