Hi, I am creating (or trying to) my first app using SwiftData - and I have questions :-)
The main question I can't get my head wrapped around is the following:
Let's say I have the sample below...
@Model class Person { @Relationship(inverse:\Hat.owner) var hat:Hat } @Model class Hat { var owner:Person? }
It looks like I am creating a strong reference cycle between the person and the hat objects? And in fact I am seeing these kinds of reference cycles when I look at the memory debugger.
Many code samples I have seen so far use this type of relationship declaration...
And I am wondering: Am I missing something? Admittedly I don't find many discussions about memory leaks caused by SwiftData despite the syntax being used in many examples?
So what is the situation? Did Apple just miss to explain that the inverse: declaration causes memory leaks or is there some kind of magic that I should understand?
No, there is no reference cycle and memory leak here. By using @Model
, hat
and owner
are converted to computed properties, and some logics are implemented there to maintain the integrity of the relationship. Those details are barely documented, but conceptually, SwiftData takes care the integrity and so you don't need to worry about that.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.