Daily iOS
Memory and ARC

Closure capture cycles

A messaging app's ViewModel keeps a completion closure in a property for a long time, and inside it updates self.messages. What reference graph creates the retain cycle?

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 ViewModel holds the closure strongly through a property, and the closure captures self strongly the moment its body uses self.messages, so the arrow from ViewModel to closure and the arrow from closure back to ViewModel form a loop and neither can be freed.

Share a question