2014-03-04 76 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

,仍然一樣... –