title: Reference Counting of self in Blocks
body: <p>I'm trying to get my head around how object lifetime and reference counting interact with code blocks. In the following code I'm just doing a simple animation that flashes as the top view on a UINavigationController's stack is swapped. The tricky part is that <em>the popped view controller is the one where this code is defined</em>.</p>
  
  <pre><code>[UIView animateWithDuration:0.2 
          animations:^{self.navigationController.view.alpha = 0.0;}
          completion:^(BOOL finished){ 
                  UINavigationController *navController = self.navigationController;  
  
                  [self.navigationController popViewControllerAnimated:NO]; 
                  [navController pushViewController:nextView animated:NO];
                  [nextView release];
  
                  [UIView animateWithDuration:0.2 
                          animations:^{navController.view.alpha = 1.0;}];                    
                   }];    
  </code></pre>
  
  <p>My question is (ignoring what the animation looks like), is this the correct way to do this from a memory management perspective. In particular:</p>
  
  <p>(1) When using this approach for the pop+push cycle, is it correct that it is no longer necessary to retain self, as in <a href="https://stackoverflow.com/questions/410471/how-can-i-pop-a-view-from-a-uinavigationcontroller-and-replace-it-with-another-in">other similar examples</a> that do not use blocks?</p>
  
  <p>(2) Does invoking animateWithDuration:... with the these blocks retain the defining view controller (self) until the blocks execute?</p>