I'm new to Swift's ARC and I'm having a possibly memory problem with the following swift code:
class Device {
let name: String
var ports: [Port]
init(name: String, ports: [Port] = []) {
self.name = name
self.ports = ports
}
}
class Port {
let name: String
let device: Device
init(name: String, device: Device) {
self.name = name
self.device = device
}
}
In the provided Swift code, a device object contains an array of ports, and each port object is associated with a specific device. A device should remain in memory as long as it's ports exist and the device of a port should never be nil. Sometimes the ports should only stay in the device's ports array, without any other reference. But with the code above, unused instances will stay in memory for ever and getting more and more.
I've already tested the following:
1.)
weak let device: Device? in the Port class. That way i can't do something like this anymore, because the Device is getting deallocated immediately (after view change):
let device = Device(name: "name")
let port = Port(name: "port", device: device)
// change view to PortView with port object / without device object --> device get's deinitalized.
2.) Weak Array (with a property wrapper). But if the ports in the array are weak, they also getting deinitalized immediately when there is no other reference to it (logically) - but sometimes there is intentionally no reference to them, so they have to stay in the array.
Where is my mistake in thinking?
Deviceinstance that holds theports). You could also use struct, or explain why Port needs to know its device? As in you sample, fromdeviceyou can't get theport.