我创建了一个"邀请朋友" iOS应用程序的功能我正在构建。我能够通过迭代大型数组来添加字母A-Z部分,使用NSPredicate选择所有以A开头然后B一直到Z的名称。将它们保存到字典中的键是A-Z。
一切正常,但是从我点击"邀请朋友"的那一刻起打开该视图大约需要7秒钟。按钮。
我可以做些什么来无缝地或至少使这种负载变得简单,使用户看起来无缝并改进设计和我的代码?
这是我的代码
-(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:)];
}
这是我称之为方法的另一个。
-(void)viewWillAppear:(BOOL)animated {
[self loadData];
}
- (void)loadData
{
[self.contactsManager importContacts:^(NSArray *contacts)
{
self.tableData = contacts;
[self testing];
[self.tableView reloadData];
NSLog(@"contacts: %@",contacts);
}];
}
谢谢!
答案 0 :(得分:2)
您基本上是遍历整个阵列26次。你需要一个只遍历一次的算法,随着时间的推移建立你的字典。
NSMutableDictionary *sectionDict = [NSMutableDictionary dictionary];
for (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];
}