What is the memory management behavior of this SwiftUI view that observes a SwiftData model object?
iOS interview question for Intermediate practice.
Answer
The code is safe; ContentView is a struct and cannot create a reference cycle with the Item object.
Explanation
This code does not have a memory management issue. ContentView is a struct, which is a value type. Value types do not participate in reference counting and therefore cannot be part of a strong reference cycle. The Item object is a class, but because the view that holds it is a struct, a cycle cannot be formed between them. The @ObservedObject correctly allows the view to observe the Item for changes without creating a memory leak.