I consider whether it is possible to return from function child object created inside parent object that will keep reference to its parent and prevent parent from being deallocated from memory. Simultaneously I don't want to have retain cycle and memory leak.
class ObjectA {
let objectB = ObjectB()
}
class ObjectB {
}
func factoryFunc() -> ObjectB {
ObjectA().objectB
}
Client code
let objB = factoryFunc()
// but keep also ObjectA as long as objB lives
// but do not create retain cycle
// I do not want to return ObjectA and do not want to manage it outside of black box factory method.
I could add
class ObjectB {
var parent: ObjectA?
}
It creates retain cycle and memory leak
class ObjectB {
weak var parent: ObjectA?
}
This doesn't prevent deallocation of ObjectA