If I comment let _ = entries
everything goes well, but if not, accessing(not editing) the collection I don't know why forces the view update (dependency change in items
) and with the view update the computed property checkingDate
is triggered firing the view update... and so on. Cycle
Why accessing entries forces view update ? If I change let _ = entries
for let _ = name
everything goes well so I conclude that is a problem with collection...
Models:
@Model
final class Item {
var name: String
@Relationship(deleteRule:.cascade) var entries: [Entry]
init(name: String) {
self.name = name
entries = []
}
}
extension Item {
@Transient var checkingDate: Date {
let _ = entries // < -- !!!
//...
return Date()
}
}
@Model final class Entry {
var value: Int
var date: Date
init(value: Int, date: Date) {
self.value = value
self.date = date
}
}
View:
struct ContentView: View {
@Query(sort: \Item.name) private var items: [Item]
@Environment(\.modelContext) private var context
var body: some View {
let _ = Self._printChanges()
List{
ForEach(items){item in
Text("Name:\(item.name), checking date: \(item.checkingDate, format: Date.FormatStyle(date: .numeric, time: .standard))")
}
}
}
}