2014-03-04 79 views
1

我想找出我的应用程序缓慢的原因。 这里就是我的视图控制器做:缓慢的iOS应用程序

  1. (viewDidLoad中)从AFNetworking web服务检索数据。
  2. 解析响应,创建自定义对象并将它们保存在字典中。
  3. 重新载入tableview数据以用自定义对象填充它。

我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,从那里读取,它仍然很慢。

谢谢

+4

“在主线程上使用AFNetworking从Web服务中检索数据”? –

+0

是的。我如何在后台运行它? –

+1

你是什么意思“慢”?你在viewDidLoad函数中有问题吗?或者你的用户界面立即打开,但数据来得晚? –

回答

0

在另一个线程中进行web服务调用。

就我个人而言,我喜欢使用Grand Central调度。这个问题using dispatch_sync in Grand Central Dispatch显示了一些很好的语法。您将所有内容放在dispatch_async部分的后台中,当服务获得数据时,您可以在dispatch_sync部分安全地更新/重新加载tableview。在这种情况下,加速与IBOutlets替换viewWithTag

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

可以使用此方法,用于后台处理。

+0

我以前试过,它没有帮助。 –

0

第一步:

+0

你认为这会在滚动速度上有显着的提高吗? –

+0

@ diogo.appDev我认为你应该尝试通过配置来查看瓶颈在哪里。使用IBOutlets的 –

+0

,仍然一样... –