title: Blocks and retain cycles
body: <p>One minor question: why is Xcode complaining that <code>listing 1</code> would lead to a retain cycle, while in <code>listing 2</code> it does not? In both cases <code>_clients</code> is an <code>int</code> instance variable. In <code>listing 2</code> it is assigned <code>0</code> in the <code>init</code> method.</p>
  
  <p>Background info: I would like to execute the loop in the block, as long as at least one client is requesting updates from the iPhone accelerometer, which I am publishing to a redis channel. If there are no more clients left, the loop would quit and stop publishing accelerometer data.</p>
  
  <p><code>Listing 2</code> comes from a small test app I wrote to verify that my idea works. <code>Listing 1</code> is implemented in the real project.</p>
  
  <h2>Listing 1</h2>
  
  <pre><code>- (id)init {
    self = [super init];
  
    if (self) {
      _clients = 0;
  
      /**
       * The callback being executed
       */
      _callback = ^ {
        while (_clients &gt; 0) { // Capturing 'self' strongly in this block is likely to lead to a retain cycle
          NSLog(@"Publish accelerometer data to redis (connected clients: %d)", _clients);
        }
      };
    }
  
    return self;
  }
  </code></pre>
  
  <h2>Listing 2</h2>
  
  <pre><code>- (void)touchedConnectButton:(id)sender {
    _clients += 1;
  
    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(concurrentQueue, ^() {
      while(_clients &gt; 0) {
        NSLog(@"Connected clients: %d", _clients);
      }
    });
  }
  </code></pre>