In my iOS app I have
class Node {
var value: String
var isExpanded: Bool
var children: [Node] = []
private var flattenElementsCache: [Node]!
// init methods
var flattenElements: [Node] {
if let cache = flattenElementsCache {
return cache
}
flattenElementsCache = []
flattenElementsCache.append(self) // (1) <-- Retain Cycle???
if isExpanded {
for child in children {
flattenElementsCache.append(contentsOf: child.flattenElements)
}
}
return flattenElementsCache;
}
}
With Instruments, I've observed some memory leaks and I think the problem is in line indicated by (1).
Could anyone explain to me if it generates a retain cycle? If yes, how do you solve it?