I was just interested to know how ARC works when a block of code goes out of scope. I assume all the reference variables are set to nil/destroyed with properties excluded and all objects are destroyed that have a 0 reference count. This scenario:
A = nil
B = nil
Or perhaps all variables and property references are set to nil going from top to bottom which would keep any retain cycles because A and B are nil before the properties are accessed.
A = nil
B = nil
A?.macbook = nil
B?.person = nil
So I was just wondering why ARC doesn't cycle through the properties first (go backwards through the code) to remove property references first, this would break any retain cycles.
A?.macbook = nil
B?.person = nil
A = nil
B = nil
I think I understand the basics of retain cycles, I'm just curious about the detail of what happens with ARC during an automated destruction phase e.g. finishing the execution of a function. Perhaps properties references aren't accessed/destroyed because they are part of the object? maybe it is too computationally complex to check every property?
var name: String
init(name: String) {
self.name = name
print("The Person \(name) is born")
}
var macbook: Macbook?
deinit {
print("The Person \(name) has died")
}
}
class Macbook {
var model: String
init(model: String) {
self.model = model
print("The \(model) Macbook is born")
}
var person: Person?
deinit {
print("The \(model) Macbook has expired")
}
}
func runTasks() {
var A: Person? = Person(name: "Reuben")
var B: Macbook? = Macbook(model: "Pro 2020")
//Setup Retain Cycle
A?.macbook = B
B?.person = A
//A?.macbook = nil
B?.person = nil //**Why doesn't ARC make this nil first to avoid retain cycles?**
A = nil
B = nil
}
runTasks()