我想找出我的应用程序慢的原因。以下是我的视图控制器所做的工作:
我的AFNetworking电话:
NSString *url = Utils urlForObjectType:objectGames;AFHTTPRequestOperationManager *manager = AFHTTPRequestOperationManager管理器;AFJSONRequestSerializer *requestSerializer = AFJSONRequestSerializer序列化程序;requestSerializer setValue:@"application/json“forHTTPHeaderField:@"Accept";manager.requestSerializer = requestSerializer;[manager GET:url参数:0成功:^(AFHTTPRequestOperation*操作,id responseObject) { self didReceiveGames:operation.responseData;} failure:^(AFHTTPRequestOperation *操作,NSError *error) {NSLog(@error:%@,error);}];
TableView方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
NSString *competition = [competitionNames objectAtIndex:indexPath.section];
NSArray *competitionGames = [competitions objectForKey:competition];
Game *game = [competitionGames objectAtIndex:indexPath.row];
Team *teamA = [game teamA];
Team *teamB = [game teamB];
UILabel *labelTeamA = (UILabel *)[cell viewWithTag:100];
UILabel *labelTeamB = (UILabel *)[cell viewWithTag:101];
UILabel *labelResultA = (UILabel *)[cell viewWithTag:102];
[labelResultA setText:nil];
UILabel *labelResultB = (UILabel *)[cell viewWithTag:103];
[labelResultB setText:nil];
UIImageView *imageTeamA = (UIImageView *)[cell viewWithTag:104];
UIImageView *imageTeamB = (UIImageView *)[cell viewWithTag:105];
UILabel *labelState = (UILabel *)[cell viewWithTag:106];
[labelState setText:nil];
GameState state = [game state];
NSString *urlImageA = [Utils urlForObjectType:objectTeamImage andID:[teamA teamID]];
NSString *urlImageB = [Utils urlForObjectType:objectTeamImage andID:[teamB teamID]];
/* Time Background */
UIImageView *timeBackground = (UIImageView *) [cell viewWithTag:107];
[timeBackground setHidden:NO];
[cell setBackgroundColor:[UIColor clearColor]];
switch (state) {
case statePlayed: {
[labelResultA setText:[NSString stringWithFormat:@"%d", [game finalResultA]]];
[labelResultB setText:[NSString stringWithFormat:@"%d", [game finalResultB]]];
[timeBackground setHidden:YES];
}
break;
case stateFixture: {
/* Set labelState to hours */
NSDate *date = [game date];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date];
NSString *minutes;
if ([components minute] == 0) {
minutes = @"00";
} else {
minutes = [NSString stringWithFormat:@"%d", [components minute]];
}
[labelState setText:[NSString stringWithFormat:@"%dh%@", [components hour], minutes]];
}
break;
case stateCancelled:
[labelState setText:@"CANCELLED"];
break;
case statePlaying: {
[labelResultA setText:[NSString stringWithFormat:@"%d", [game finalResultA]]];
[labelResultB setText:[NSString stringWithFormat:@"%d", [game finalResultB]]];
[timeBackground setHidden:YES];
}
break;
case statePostponed:
[labelState setText:@"POSTPONED"];
break;
case stateSuspended:
[labelState setText:@"SUSPENSED"];
break;
default:
break;
}
[labelTeamA setText:[teamA name]];
[labelTeamB setText:[teamB name]];
[imageTeamA setImageWithURL:[NSURL URLWithString:urlImageA]];
[imageTeamB setImageWithURL:[NSURL URLWithString:urlImageB]];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *competitionGames = [competitions objectForKey:[competitionNames objectAtIndex:section]];
return [competitionGames count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [competitionNames count];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *header = [[NSBundle mainBundle] loadNibNamed:@"GamesHeaderIPAD" owner:self options:nil][0];
NSString *competitionName = [[competitionNames objectAtIndex:section] uppercaseString];
UILabel *labelCompetition = (UILabel *)[header viewWithTag:100];
[labelCompetition setText:competitionName];
return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 42;
}编辑:与AFNetworking无关,我只是在NSUserDefaults中保存了响应,从那里读取它,它仍然很慢。
谢谢
发布于 2014-03-04 11:21:21
在另一个线程中调用webservice。
就我个人而言,我喜欢使用中央调度。这个问题sync in Grand Central Dispatch展示了一些很好的语法。将所有要运行的内容放在dispatch_async部件的后台,当服务获得它的数据时,您可以在dispatch_sync部件中安全地更新/重新加载表视图。
发布于 2014-03-04 11:30:59
您可以将此方法用于背景处理:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSString *url = [Utils urlForObjectType:objectGames];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = requestSerializer;
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation,id responseObject) {
dispatch_async(dispatch_get_main_queue(), ^{
[self didReceiveGames:operation.responseData];
}];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[self didReceiveGames:operation.responseData];
}];
NSLog(@"Error: %@", error);
}];
});发布于 2014-03-04 11:47:05
在这种情况下加快速度的第一步是将viewWithTag替换为IBOutlets。
https://stackoverflow.com/questions/22170206
复制相似问题