2014-11-17 2 views
1

완료 블록 내에서 메소드를 실행하는 올바른 방법은 무엇입니까 (권장되는 경우라도). 지금은 정보가 성공적으로 검색되었는지 여부를 나타내는 완료 블록으로 정보를 다운로드하는 메서드를 호출하는 IBAction이 있습니다. 그럴 경우 해당 정보를 표시 할보기 컨트롤러를 밀어 넣으려고하지만 현재로서는 아무 것도 일어나지 않습니다. 나는블록 내부의 뷰 컨트롤러를 밀어 넣기

__weak YTTMSetupViewController *weakSelf = self; 
    [mc downloadJson:^(BOOL success) { 
      if(success){ 
       NSLog(@"sucess. metric count - %i",(int)mc.collection.count); 

       //info was downloaded. Push new view controller with info 
       YTTMMetricTableViewController *mtvc = [self.storyboard instantiateViewControllerWithIdentifier:@"YTTMMetricTableViewController"]; 
       mtvc.group = (WAGroup*)[[WAMetricCollection sharedInstance].collection lastObject]; 
       mtvc.hidesBottomBarWhenPushed = YES; 
       [weakSelf.navigationController pushViewController:mtvc animated:YES]; 
      } 
      else{ 
       NSLog(@"failure"); 
       //display failure UI 
      } 
      NSLog(@"end of downloading"); 
      [HUD dismissAfterDelay:0.5f animated:YES]; 
     }]; 
+0

당신은 지연 0.0 초에 dispatch_asynch에서과 pushViewController 전화를 시도 할 수 있습니다. 일반적으로 작동합니다. – Dzmitry

답변

1

이 그것을 할 올바른 방법인지 확실하지 ... 그것은 등 주요 스레드, GCD, 함께 할 수있는 뭔가가 추측하고있어,하지만했다.

 [weakSelf performSelectorOnMainThread:@selector(pushDetail) withObject:nil waitUntilDone:YES]; 

완료 코드 :

__weak YTTMSetupViewController *weakSelf = self; 
    [mc downloadJson:^(BOOL success) { 
      if(success){ 
       NSLog(@"sucess. metric count - %i",(int)mc.collection.count); 

       //info was downloaded. Push new view controller with info 
       [weakSelf performSelectorOnMainThread:@selector(pushDetail) withObject:nil waitUntilDone:YES]; 
      } 
      else{ 
       NSLog(@"failure"); 
       //display failure UI 
      } 
      NSLog(@"end of downloading");    
     }]; 

} 

-(void)pushDetail{ 
    __weak YTTMSetupViewController *weakSelf = self; 
    YTTMMetricTableViewController *mtvc = [self.storyboard instantiateViewControllerWithIdentifier:@"YTTMMetricTableViewController"]; 
    mtvc.group = (WAGroup*)[[WAMetricCollection sharedInstance].collection lastObject]; 
    mtvc.hidesBottomBarWhenPushed = YES; 
    [weakSelf.navigationController pushViewController:mtvc animated:YES]; 

} 
0

모든 UI 업데이트는 메인 스레드에서 수행해야

나는 그렇게으로 주 스레드에서 벤처 캐피탈을 밀어하는 방법을 추가했다. 개인적으로 보다 읽기 쉬운 코드를 생성하므로 GCD을 통해이 작업을 수행하는 것이 좋습니다. 그러나 일부 완료 블록을 실행 한 후 주 스레드에서 단일 UI 업데이트를 호출하는 경우 개인적인 취향과는 별도로 performSelectorOnMainThread과 관련된 문제는 없습니다. 어느 쪽을 선택하든, 당신은 be consistent with what you use to guarantee that blocks are enqueued in the order you specified이어야합니다.

그러나 워킹 코드를 제외하고 Apple의 프레임 워크는 메서드 매개 변수로 대기열을 지정하지 않으면 주 스레드에서 모든 완료 블록을 수행하는 것처럼 보입니다.이 경우 완료 블록은 해당 대기열에서 수행되어야합니다. 따라서이 경우 다운로드 핸들러 클래스의 downloadJson 메서드를 편집하여 주 큐의 완료 블록을 자동으로 수행하는 것이 좋습니다.

1

당신은 단순히 dispatch_asynch 블록과 호출을 래핑 시도해 볼 수도 있습니다 ...

__weak YTTMSetupViewController *weakSelf = self; 
    [mc downloadJson:^(BOOL success) { 
     if(success){ 
      NSLog(@"sucess. metric count - %i",(int)mc.collection.count); 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       //info was downloaded. Push new view controller with info 
       YTTMMetricTableViewController *mtvc = [self.storyboard instantiateViewControllerWithIdentifier:@"YTTMMetricTableViewController"]; 
       mtvc.group = (WAGroup*)[[WAMetricCollection sharedInstance].collection lastObject]; 
       mtvc.hidesBottomBarWhenPushed = YES; 
       [weakSelf.navigationController pushViewController:mtvc animated:YES]; 
      }); 
     } 
     else{ 
      NSLog(@"failure"); 
      //display failure UI 
     } 
     NSLog(@"end of downloading"); 
     [HUD dismissAfterDelay:0.5f animated:YES]; 
    }];