ios - Faster way to iterate through a large NSArray of dictionaries -
i created "invite friends" feature ios app i'm building. able add alphabetical sections a-z, iterating through large array, using nspredicate picking out names start b way z. saving them dictionary keys a-z.
everything works, opening view takes 7 seconds moment click "invite friends" button.
what can make load seamlessly or atleast make seem seamless users , improving design , code?
here code
-(void)testing { nsmutablearray *stringname = [[nsmutablearray alloc] init]; (nsdictionary* item in self.tabledata) { // self.user = item; nsstring* firstname = [item objectforkey:@"firstname"]; [stringname addobject:firstname]; nspredicate *apredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'a'"]; nsarray *beginwitha = [stringname filteredarrayusingpredicate:apredicate]; nspredicate *bpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'b'"]; nsarray *beginwithb = [stringname filteredarrayusingpredicate:bpredicate]; nspredicate *cpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'c'"]; nsarray *beginwithc = [stringname filteredarrayusingpredicate:cpredicate]; nspredicate *dpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'd'"]; nsarray *beginwithd = [stringname filteredarrayusingpredicate:dpredicate]; nspredicate *epredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'e'"]; nsarray *beginwithe = [stringname filteredarrayusingpredicate:epredicate]; nspredicate *fpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'f'"]; nsarray *beginwithf = [stringname filteredarrayusingpredicate:fpredicate]; nspredicate *gpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'g'"]; nsarray *beginwithg = [stringname filteredarrayusingpredicate:gpredicate]; nspredicate *hpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'h'"]; nsarray *beginwithh = [stringname filteredarrayusingpredicate:hpredicate]; nspredicate *ipredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'i'"]; nsarray *beginwithi = [stringname filteredarrayusingpredicate:ipredicate]; nspredicate *jpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'j'"]; nsarray *beginwithj = [stringname filteredarrayusingpredicate:jpredicate]; nspredicate *kpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'k'"]; nsarray *beginwithk = [stringname filteredarrayusingpredicate:kpredicate]; nspredicate *lpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'l'"]; nsarray *beginwithl = [stringname filteredarrayusingpredicate:lpredicate]; nspredicate *mpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'm'"]; nsarray *beginwithm = [stringname filteredarrayusingpredicate:mpredicate]; nspredicate *npredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'n'"]; nsarray *beginwithn = [stringname filteredarrayusingpredicate:npredicate]; nspredicate *opredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'o'"]; nsarray *beginwitho = [stringname filteredarrayusingpredicate:opredicate]; nspredicate *ppredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'p'"]; nsarray *beginwithp = [stringname filteredarrayusingpredicate:ppredicate]; nspredicate *qpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'q'"]; nsarray *beginwithq = [stringname filteredarrayusingpredicate:qpredicate]; nspredicate *rpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'r'"]; nsarray *beginwithr = [stringname filteredarrayusingpredicate:rpredicate]; nspredicate *spredicate = [nspredicate predicatewithformat:@"self beginswith[c] 's'"]; nsarray *beginwiths = [stringname filteredarrayusingpredicate:spredicate]; nspredicate *tpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 't'"]; nsarray *beginwitht = [stringname filteredarrayusingpredicate:tpredicate]; nspredicate *upredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'u'"]; nsarray *beginwithu = [stringname filteredarrayusingpredicate:upredicate]; nspredicate *vpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'v'"]; nsarray *beginwithv = [stringname filteredarrayusingpredicate:vpredicate]; nspredicate *wpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'w'"]; nsarray *beginwithw = [stringname filteredarrayusingpredicate:wpredicate]; nspredicate *xpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'x'"]; nsarray *beginwithx = [stringname filteredarrayusingpredicate:xpredicate]; nspredicate *ypredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'y'"]; nsarray *beginwithy = [stringname filteredarrayusingpredicate:ypredicate]; nspredicate *zpredicate = [nspredicate predicatewithformat:@"self beginswith[c] 'z'"]; nsarray *beginwithz = [stringname filteredarrayusingpredicate:zpredicate]; // dictionary self.sectiondict = [[nsmutabledictionary alloc] init]; if (![beginwitha count] == 0) { _sectiondict[@"a"] = beginwitha; } if (![beginwithb count] == 0) { _sectiondict[@"b"] = beginwithb; } if (![beginwithc count] == 0) { _sectiondict[@"c"] = beginwithc; } if (![beginwithd count] == 0) { _sectiondict[@"d"] = beginwithd; } if (![beginwithe count] == 0) { _sectiondict[@"e"] = beginwithe; } if (![beginwithf count] == 0) { _sectiondict[@"f"] = beginwithf; } if (![beginwithg count] == 0) { _sectiondict[@"g"] = beginwithg; } if (![beginwithh count] == 0) { _sectiondict[@"h"] = beginwithh; } if (![beginwithi count] == 0) { _sectiondict[@"i"] = beginwithi; } if (![beginwithj count] == 0) { _sectiondict[@"j"] = beginwithj; } if (![beginwithk count] == 0) { _sectiondict[@"k"] = beginwithk; } if (![beginwithl count] == 0) { _sectiondict[@"l"] = beginwithl; } if (![beginwithm count] == 0) { _sectiondict[@"m"] = beginwithm; } // if (![beginwithn count] == 0) { _sectiondict[@"n"] = beginwithn; } if (![beginwitho count] == 0) { _sectiondict[@"o"] = beginwitho; } if (![beginwithp count] == 0) { _sectiondict[@"p"] = beginwithp; } if (![beginwithq count] == 0) { _sectiondict[@"q"] = beginwithq; } if (![beginwithr count] == 0) { _sectiondict[@"r"] = beginwithr; } if (![beginwiths count] == 0) { _sectiondict[@"s"] = beginwiths; } if (![beginwitht count] == 0) { _sectiondict[@"t"] = beginwitht; } if (![beginwithu count] == 0) { _sectiondict[@"u"] = beginwithu; } if (![beginwithv count] == 0) { _sectiondict[@"v"] = beginwithv; } if (![beginwithw count] == 0) { _sectiondict[@"w"] = beginwithw; } if (![beginwithx count] == 0) { _sectiondict[@"x"] = beginwithx; } if (![beginwithy count] == 0) { _sectiondict[@"y"] = beginwithy; } if (![beginwithz count] == 0) { _sectiondict[@"z"] = beginwithz; } } self.dictsectiontitles = [[self.sectiondict allkeys] sortedarrayusingselector:@selector(localizedcaseinsensitivecompare:)]; } here other in call method.
-(void)viewwillappear:(bool)animated { [self loaddata]; } - (void)loaddata { [self.contactsmanager importcontacts:^(nsarray *contacts) { self.tabledata = contacts; [self testing]; [self.tableview reloaddata]; nslog(@"contacts: %@",contacts); }]; } thanks!
you traversing entire array 26 times. need algorithm traverses once, building dictionary go along.
nsmutabledictionary *sectiondict = [nsmutabledictionary dictionary]; (nsstring *s in array) { nsstring *firstletter = [s substringtoindex:1]; nsmutablearray *section = sectiondict[firstletter]; if (section == nil) { section = [nsmutablearray array]; sectiondict[firstletter] = section; } section addobject:s]; }
Comments
Post a Comment