A retain cycle is an ownership graph that cannot release itself. Weak references are useful only when they match the desired lifetime; adding weak everywhere can hide missing ownership.
- 1Owner
- 2Stored closure or subscription
- 3Captured owner
- 4Break intended edge
Work through the example
Draw the ownership graph before changing a capture list. Confirm deallocation after the screen or task ends.
Start with a disposable branch and synthetic data. Write the expected outcome before changing the implementation, then keep the first failing result. This prevents a later repair from quietly redefining the task. The procedure below is grounded in the repository reference; its examples must still be checked against your project and installed toolchain.
Implementation reference
The following focused section is adapted from the maintained project guide. It preserves the source’s examples and limitations.
Common retain-cycle shapes
Delegate cycles:
protocol PlayerCoordinatorDelegate: AnyObject {
func playerDidFinish()
}
final class PlayerCoordinator {
weak var delegate: PlayerCoordinatorDelegate?
}
Closure cycles:
final class Loader {
private var onComplete: (() -> Void)?
func start() {
onComplete = { [weak self] in
self?.finish()
}
}
private func finish() {}
}
Timer cycles:
final class Poller {
private var timer: Timer?
func start() {
timer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { [weak self] _ in
self?.tick()
}
}
func stop() {
timer?.invalidate()
timer = nil
}
deinit {
timer?.invalidate()
}
private func tick() {}
}
Combine cycles:
final class SearchModel {
private var cancellables = Set<AnyCancellable>()
func bind(_ publisher: AnyPublisher<String, Never>) {
publisher
.sink { [weak self] query in
self?.runSearch(query)
}
.store(in: &cancellables)
}
private func runSearch(_ query: String) {}
}
Acceptance and failure review
| Checkpoint | What to inspect | If it does not match |
|---|---|---|
| Owner | Confirm the input and environment | Preserve the failure and return to this step |
| Stored closure or subscription | Inspect the intermediate artifact | Preserve the failure and return to this step |
| Captured owner | Run the focused check | Preserve the failure and return to this step |
| Break intended edge | Record the observed result | Preserve the failure and return to this step |
Ask the agent to explain the smallest change that resolves the observed mismatch. Keep unrelated refactors out of the repair. A change that makes a warning disappear is not enough if the behavior or ownership contract has changed. Re-run the same acceptance check so the before and after results are comparable.
Evidence and limits
Static review findings require runtime confirmation when lifetime depends on navigation or cancellation.
This is an educational guide. Its presence in the series does not certify a completed client-specific lab. The series evidence record separates executed checks from exercises and blocked environments.
Inspect the source used in this lesson.
Related reading
- How can I catch Swift concurrency review findings before accepting an agent’s changes?
A real before-and-after MCP review with file and line evidence, plus the limits of text-based concurrency checks.
- Review AI-generated Swift before you trust it
A focused review, a small patch and a real test beat a confident completion message.
What to do next
Next: Security review of an agent-built app: keychain, ATS, secrets in source