2013-01-23 1 views
0

아주 이상한 행동을 눈치 채고 있습니다. 다른 누구와도 만나기를 기대하고 있습니다. 비동기 API 호출 (아래 코드)을 만들고 있습니다. 호출이 완료되면 호출 결과에서 배열이 채워진 다음 내 테이블의 뷰를 배열의 데이터로 업데이트해야하는 테이블을 다시로드합니다 (cellForRowAtIndexPath이 호출되어야 함). 그러나 테이블 뷰의 데이터는 다른 방법으로 다시로드해야 할 때까지 표시되지 않습니다. 예를 들어 탭을 클릭 한 다음 원래 뷰로 돌아가서 뷰를 변경하는 경우입니다. "테이블 새로 고침"의 일부 측면이있는 것처럼 보이지만 비동기 호출이 반환 될 때 reloadData을 호출합니다.비동기 호출 표가 새로 고침되지 않습니다.

코드 :

-(void)refreshWeeksOffers 
{ 
    [array removeAllObjects]; 

    NSMutableURLRequest *request = 
     [WebRequests createPostRequestWithApiCall:@"getResults" bodyData:@"params={\"locale\" : \"US\"}"]; 

    [NSURLConnection 
    sendAsynchronousRequest:request 
    queue:[[NSOperationQueue alloc] init] 
    completionHandler:^(NSURLResponse *response, 
         NSData *data, 
         NSError *error) 
    { 
     if ([data length] >0 && error == nil) 
     { 
      // parse home page offers from resulting json 
      JsonParser *parser = [[JsonParser alloc] initWithData:data]; 
      array = [parser parseHomepageResults]; 

      [self.topWeekTable reloadData]; 

     } 
     else if ([data length] == 0 && error == nil) 
     { 
      NSLog(@"Nothing was downloaded."); 
     } 
     else if (error != nil){ 
      NSLog(@"Error = %@", error); 
     } 

    }]; 

    [self.topWeekTable reloadData]; 
} 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [array count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if(!cell) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    Offer *currentOffer = (Offer *)[array objectAtIndex:indexPath.row]; 

    cell.textLabel.text = [NSString stringWithFormat:@"%.1f%% Back", currentOffer.advertisedRate]; 

    NSData *data = [NSData dataWithContentsOfURL:currentOffer.storeImage]; 
    UIImage *img = [[UIImage alloc] initWithData:data]; 

    cell.imageView.image = img; 

    return cell; 
} 

답변

1

지원되지 않는 백그라운드 스레드에서 UIKit를 호출하는 때문입니다.

이 시도 :

[ self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO ] ; 

나는이처럼 선호하는 또 다른 전략 :

@implementation NSThread (BlockPerforming) 

-(void)performBlock:(void(^)())block 
{ 
    if (!block) { return ; } 
    [ self performSelector:@selector(performBlock:) onThread:self withObject:[ block copy ] waitUntilDone:NO ] ; 
} 

@end 
:

-(void)startAsyncSomething 
{ 
    [ obj operationWithAsyncHandler:^{ 
     [ [ NSThread mainThread ] performBlock:^{ 
      ... handle completion here ... 
     } ] 
    }] 
} 

당신은이 같은 범주 뭔가 에 NSThread-performBlock:을 추가 할 수

+0

고마워, 그건 속임수 였어! –

+0

첫 번째 해결책은 좀 더 복잡한 상황에서 성가시다. 그래서 나는 편집으로 추가 한 두 번째 방법을 선호한다. – nielsbot

관련 문제