慢iOS应用程序

时间:2014-03-04 11:05:26

标签: ios performance uitableview

我想找出我的申请速度慢的原因。 这是我的视图控制器正在做的事情:

  1. (viewDidLoad)使用AFNetworking从Web服务中检索数据。
  2. 解析响应,创建自定义对象并将其保存在字典中。
  3. 重新加载tableview数据以使用自定义对象填充它。
  4. 我的AFNetworking电话:

    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) {
        [self didReceiveGames:operation.responseData];
    } failure:^(AFHTTPRequestOperation *operation, 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中保存了响应,从那里读取它仍然很慢。

    由于

3 个答案:

答案 0 :(得分:0)

在另一个线程中进行webservice调用。

就个人而言,我喜欢使用Grand Central派遣。这个问题using dispatch_sync in Grand Central Dispatch显示了一些好的语法。您可以将所有内容放在dispatch_async部分的后台运行,当服务获得数据时,您可以在dispatch_sync部分安全地更新/重新加载您的tableview。

答案 1 :(得分:0)

您可以使用此方法进行后台处理:

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);
       }];
    });

答案 2 :(得分:0)

在这种情况下加速的第一步是用IBOutlets替换viewWithTag。