Daily iOS
Memory and ARC

Stored closures and self lifetime

A UIKit ViewController keeps an upload completion block in a property, and the block changes one of the ViewController's labels. After the upload finishes the closure is still sitting in that property. How would you break the loop between it and self?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

The surest way is to assign nil to the property right after the closure runs, removing the arrow from ViewController to closure; and to cover paths where it never runs (failure, leaving the screen), capture [weak self] in the closure so the reverse arrow is weak too, blocking it twice over.

Share a question