2014-03-04 4 views
1

내 응용 프로그램이 느린 이유를 알고 싶습니다. 여기 내보기 컨트롤러가하고있는 작업은 다음과 같습니다느린 iOS 응용 프로그램

  1. (있는 viewDidLoad는) AFNetworking와 웹 서비스에서 데이터를 검색합니다.
  2. 응답을 구문 분석하고 사용자 지정 개체를 만들어 사전에 저장합니다.
  3. 테이블 뷰 데이터를 다시로드하여 사용자 지정 개체로 채 웁니다.

내 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 방법 : 는이 AFNetworking 함께 할 수 없다, 난 그냥에서 응답을 저장 : 편집

- (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; 
} 

NSUserDefaults, 거기에서 읽으면 여전히 느립니다.

감사합니다.

+4

메인 스레드에서 "AFNetworking을 사용하여 웹 서비스에서 데이터 검색"? –

+0

예. 백그라운드에서 실행하려면 어떻게해야합니까? –

+1

"천천히"란 무엇을 의미합니까? viewDidLoad 함수에 문제가 있습니까? 또는 귀하의 UI가 즉시 열리지 만 나중에 데이터가 나타 납니까? –

답변

0

다른 스레드에서 웹 서비스 호출을합니다.

저는 개인적으로 그랜드 센트럴 디스패치를 ​​사용하고 싶습니다. 이 질문 using dispatch_sync in Grand Central Dispatch은 좋은 구문을 보여줍니다. dispatch_async 부분에 백그라운드에서 실행되도록 모든 것을 배치합니다. 서비스에 데이터가있는 경우 dispatch_sync 부분에서 안전하게 tableview를 업데이트/다시로드 할 수 있습니다.

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

이전에 시도해 보았습니다. 도움이 안돼. –

0

첫 단계 IBOutlets viewWithTag으로 대체되는 경우 가속화된다.

+0

스크롤 속도가 크게 향상 될 것이라고 생각하십니까? –

+0

@ diogo.appDev 병목 현상이 어디 있는지 프로필을 작성해야한다고 생각합니다. –

+0

여전히 똑같은 IBOutlets 사용 ... –