What if your app secretly keeps holding onto old data forever, slowing everything down without you knowing?
Why Strong reference cycles between classes in Swift? - Purpose & Use Cases
Imagine you have two friends who always hold hands tightly. They never let go, even when they want to do other things. In programming, this is like two classes that keep strong references to each other, never letting go.
When two classes hold strong references to each other, they create a loop. This loop means neither class can be cleaned up or removed from memory. Over time, this causes your app to use more and more memory, slowing it down or even crashing.
By understanding strong reference cycles, you can use special tools like weak or unowned references. These let one class hold a lighter grip, breaking the loop and allowing memory to be freed properly.
class A { var b: B? } class B { var a: A? }
class A { var b: B? } class B { weak var a: A? }
It lets your app run smoothly by preventing memory leaks and keeping your program efficient.
Think of a parent and child relationship in a family tree app. The parent holds a strong reference to the child, but the child holds a weak reference back to the parent to avoid a memory loop.
Strong reference cycles cause memory leaks by trapping objects.
Using weak or unowned references breaks these cycles.
Breaking cycles keeps apps fast and stable.
Practice
What causes a strong reference cycle between two Swift classes?
Consider two classes holding references to each other.
Solution
Step 1: Understand strong references
Strong references keep objects alive by increasing their reference count.Step 2: Identify cycle condition
If two objects hold strong references to each other, neither can be deallocated, causing a cycle.Final Answer:
Both classes hold strong references to each other, preventing deallocation. -> Option CQuick Check:
Strong references + mutual hold = cycle [OK]
- Thinking weak references cause cycles
- Assuming one strong and one weak causes cycle
- Confusing unowned with strong references
Which of the following is the correct way to declare a weak reference in Swift to avoid a strong reference cycle?
class Person {
var apartment: Apartment?
}
class Apartment {
// What should go here?
var tenant: ???
}Solution
Step 1: Identify weak reference syntax
In Swift, weak references are declared with the keywordweakand must be optional.Step 2: Apply to property
So,weak var tenant: Person?is correct to avoid strong cycles.Final Answer:
weak var tenant: Person? -> Option BQuick Check:
Weak + optional = correct syntax [OK]
- Declaring weak without optional type
- Using unowned when weak is needed
- Forgetting 'weak' keyword
What will be the output of the following Swift code?
class Person {
let name: String
var apartment: Apartment?
init(name: String) { self.name = name }
deinit { print("Person \(name) is being deinitialized") }
}
class Apartment {
let unit: String
weak var tenant: Person?
init(unit: String) { self.unit = unit }
deinit { print("Apartment \(unit) is being deinitialized") }
}
var john: Person? = Person(name: "John")
var apt: Apartment? = Apartment(unit: "4A")
john!.apartment = apt
apt!.tenant = john
john = nil
apt = nilSolution
Step 1: Analyze references
Person holds strong reference to Apartment; Apartment holds weak reference to Person.Step 2: Check deallocation
Setting both variables to nil breaks strong references, so both objects deinit and print messages.Final Answer:
Person John is being deinitialized Apartment 4A is being deinitialized -> Option AQuick Check:
Weak reference avoids cycle, both deinit [OK]
- Expecting memory leak output
- Thinking only one object deinitializes
- Ignoring weak reference effect
Identify the error in the following Swift code that causes a strong reference cycle:
class Customer {
let name: String
var card: CreditCard?
init(name: String) { self.name = name }
deinit { print("Customer \(name) is being deinitialized") }
}
class CreditCard {
let number: UInt64
var customer: Customer?
init(number: UInt64) { self.number = number }
deinit { print("CreditCard \(number) is being deinitialized") }
}
var alice: Customer? = Customer(name: "Alice")
var card: CreditCard? = CreditCard(number: 1234567890123456)
alice!.card = card
card!.customer = alice
alice = nil
card = nilSolution
Step 1: Identify reference types
Both Customer.card and CreditCard.customer are strong references by default.Step 2: Find cycle cause
Since both hold strong references, they create a cycle preventing deallocation.Step 3: Fix cycle
Declaring CreditCard.customer asweak var customer: Customer?breaks the cycle.Final Answer:
CreditCard's customer property should be declared weak to avoid cycle. -> Option AQuick Check:
Weak reference breaks cycle [OK]
- Making both references weak
- Making the wrong property weak
- Ignoring the cycle causing deallocation failure
You have two classes, Owner and Car. The Owner has a strong reference to Car, and Car has a strong reference to Owner. You want to avoid a strong reference cycle but also ensure that Car always has an Owner during its lifetime. How should you declare the reference from Car to Owner?
Solution
Step 1: Understand ownership requirements
SinceCarmust always have anOwnerduring its lifetime, the reference cannot be optional.Step 2: Choose reference type
unownedreferences are non-optional and do not increase reference count, avoiding cycles.Step 3: Apply declaration
Declareunowned var owner: OwnerinCarto avoid cycle and ensure non-optional reference.Final Answer:
Declare the reference as unowned var owner: Owner -> Option DQuick Check:
Unowned + non-optional = correct for guaranteed lifetime [OK]
- Using weak with non-optional type (invalid)
- Using strong reference causing cycle
- Using weak optional when non-optional needed
Ready to prove it?
Pass the test and this pattern moves unknown
