Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another option would be:</p> <pre class="lang-c prettyprint-override"><code>NSOperationSubclass *operation = [[NSOperationSubclass alloc] init]; __weak NSOperationSubclass *weakOperation = operation; [operation setCompletionBlock:^{ NSOperationSubclass *strongOperation = weakOperation; dispatch_async(dispatch_get_main_queue(), ^{ assert(strongOperation != nil); ... }); }]; [operationQueue addOperation:operation]; </code></pre> <p>I assume you also add operation object to an NSOperationQueue. In that case, the queue is retaining an operation. It is probably also retaining it during execution of the completion block (although I haven't found official confirmation about the completion block).</p> <p>But inside you completion block another block is created. That block will be run at some point in time later, possibly after NSOperations's completion block is run to an end. When this happens, <code>operation</code> will be released by the queue and <code>weakOperation</code> will be <code>nil</code>. But if we create another strong reference to the same object from operation's completion block, we'll make sure <code>operation</code> will exist when the second block is run, and avoid retain cycle because we don't capture <code>operation</code> variable by the block.</p> <p>Apple provides this example in <a href="http://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html" rel="noreferrer">Transitioning to ARC Release Notes</a> see the last code snippet in <em>Use Lifetime Qualifiers to Avoid Strong Reference Cycles</em> section.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload