I created a "invite friends" feature for a iOS app I'm building. I was able to add alphabetical sections A-Z, by iterating through the large array, using NSPredicate picking out all names that start with A then B all the way to Z. Saving them into a dictionary were the keys are A-Z.
Everything works, but opening that view takes about 7 seconds from the moment I click the "invite friends" button.
What can I do to make this load seamlessly or atleast make it seem seamless to the users and improving the design and my code?
Here is my code
-(void)testing {
NSMutableArray *stringName = [[NSMutableArray alloc] init];
for (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 is the other in which I call the 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!