1

내 코드에서 내 블록의 유지 사이클을 찾고 있습니다. 내 UITableViewController에 다음 코드가 있습니다블록의 중첩 된 블록에 대한 약한 참조가 필요할 때

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //[...] Not important code. 
    __weak ELParkingLocationTVC *weakself = self; // Required? (1) 
    cell.completionBlock =^{ 
     __strong ELParkingLocationTVC *strongSelf = weakself; // Required? (2) 
     __weak ELParkingLocationTVC *weak2self = weakself; // Required? (3) 
     [strongSelf dismissViewControllerAnimated:YES completion:^{ 
      __strong ELParkingLocationTVC *strong2self = weak2self; //Required? (4) 
      ELMenuHistoryTVC *menuTVC = [strong2self.navigationController.viewControllers firstObject]; 
      [menuTVC.parentTabBarController moveToTab:ELMapViewControllerIndex completion:^(BOOL finished) { 
       ElMainMapViewController *mainMapViewController = menuTVC.parentTabBarController.viewControllers[ELMapViewControllerIndex]; 
       //ELCustomParkplace *customParkplace = [strong2self parkplaceAtIndex:indexPath.row]; //(5) 
       [mainMapViewController moveToCoordinate:customParkplace.coordinate]; 
      }]; 
     }]; 
    }; 
} 

을 그리고 다음과 같은 질문이 있습니다

  1. __weak 내가 __strong 참조가 필요합니까를? 제 1과 제 2가 사용해야한다고 확신하지만, 제 3과 4에 대해서는 정말로 확신하지 못합니다.
  2. 여기서 @strongify@weakify을 사용할 수 있습니까?
  3. 줄 (5)이 필요 없다고 생각하지만 잘 모르겠습니다.

어떤 제안, 링크 또는 좋은 의견을 주시면 감사하겠습니다!

+0

이 기사를 시도하십시오 http://dhoerl.wordpress.com/2013/04/23/i-finally-figured-out-weakself-and-strongself/ – l0gg3r

+0

저는 기본 사항을 이미 알고 있지만 링크 (특히' __typeof __ (self)':-) – Szu

답변

3

참조주기가 있으므로 self > UITableView > UITableViewCell > self이므로 셀 완료 핸들러에 약한 참조가 필요합니다.

__strong 한정자를 사용할 필요가 없습니다. 변수는 기본적으로 강력한 참조입니다.

애니메이션 및 전환 완료 핸들러는 전환이 완료되는 즉시 해제되므로 약 참조는 필요하지 않지만이 경우 블록 안에 중첩되어 있기 때문에 self을 사용할 수 없습니다을 캡처 할 수 없습니다.

따라서 weakSelfstrongSelf을 유지하고 모든 내포 된 블록 내에서 동일한 strongSelf 변수를 사용하십시오.