I created FB13074428 and a small sample project to demonstrate the bug. https://github.com/jamiemcd/Apple-FB13074428
Basically, if a SwiftData class is marked as @Model and it has a computed property that depends on a relationship's stored property, the computed property does not trigger updates in SwiftUI when the underlying relationship changes its stored property. This is Xcode 15 Beta 8.
@Model
final class Airport {
var code: String
var name: String
var airplanes: [Airplane]
init(code: String, name: String, airPlanes: [Airplane]) {
self.code = code
self.name = name
self.airplanes = airPlanes
}
// Bug: This computed property is not triggering observation in AirportView when airplane.state changes. My understanding of the new observation framework is that it should.
var numberOfAirplanesDeparting: Int {
var numberOfAirplanesDeparting = 0
for airplane in airplanes {
if airplane.state == .departing {
numberOfAirplanesDeparting += 1
}
}
return numberOfAirplanesDeparting
}
}
AirportView should update because it has Text for airport.numberOfAirplanesDeparting
struct AirportView: View {
var airport: Airport
var body: some View {
List {
Section {
ForEach(airport.airplanes) { airplane in
NavigationLink(value: airplane) {
HStack {
Image(systemName: airplane.state.imageSystemName)
VStack(alignment: .leading) {
Text(airplane.name)
Text(airplane.type.name)
}
}
}
}
} header: {
Text(airport.name)
} footer: {
VStack(alignment: .leading) {
Text("Planes departing = \(airport.numberOfAirplanesDeparting)")
Text("Planes in flight = \(airport.numberOfAirplanesInFlight)")
Text("Planes landing = \(airport.numberOfAirplanesLanding)")
}
}
}
}