Retain cycles and how to break them
WHAT IT TESTS: ARC and closure capture. OUTLINE: a cycle is two objects (or a closure and its owner) holding mutual strong references so neither deallocates; break it with a [weak] or [unowned] capture list.
WHAT IT TESTS: understanding ARC and the most common leak source on iOS. ANSWER OUTLINE: a retain cycle occurs when objects hold strong references to each other so their reference counts never reach zero and none deallocate; closures cause this by strongly capturing self while self stores the closure. Break it with a capture list, using [weak self] when the reference may become nil or [unowned self] only when self is guaranteed to outlive the closure. RED FLAG: reaching for unowned where a nil access could crash.
Read the original → interview
- #swift
- #arc
- #memory
- #closures
- #retain-cycle
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.