Query not updating #313
This works perfectly on initialization and the two pickers are correctly populated and selected binding are assigned.
Here are printouts from the console. Initial Presentation0.000s PRAGMA query_only = 1
0.000s BEGIN DEFERRED TRANSACTION
0.000s SELECT "rooms"."id", "rooms"."name", "rooms"."location", "rooms"."propertyID" FROM "rooms" LIMIT 1
0.000s COMMIT TRANSACTION
0.000s PRAGMA query_only = 0
0.000s PRAGMA query_only = 1
0.000s BEGIN DEFERRED TRANSACTION
0.000s SELECT "rooms"."id", "rooms"."name", "rooms"."location", "rooms"."propertyID" FROM "rooms" WHERE ("rooms"."propertyID") = (1) ORDER BY "rooms"."name"
0.000s COMMIT TRANSACTION
0.000s PRAGMA query_only = 0
currentProperty.id: 1
0.000s PRAGMA query_only = 1
0.000s BEGIN DEFERRED TRANSACTION
0.000s SELECT "rooms"."id", "rooms"."name", "rooms"."location", "rooms"."propertyID" FROM "rooms" LIMIT 1
0.000s COMMIT TRANSACTION
0.000s PRAGMA query_only = 0
0.000s PRAGMA query_only = 1
0.000s BEGIN DEFERRED TRANSACTION
0.000s SELECT "rooms"."id", "rooms"."name", "rooms"."location", "rooms"."propertyID" FROM "rooms" WHERE ("rooms"."propertyID") = (1) ORDER BY "rooms"."name"
0.000s COMMIT TRANSACTION
0.000s PRAGMA query_only = 0After changing propertyWhen I change a property, you can see that the currentProperty.id changes, but the query is still using the previous one. currentProperty.id: 2
0.000s PRAGMA query_only = 1
0.000s BEGIN DEFERRED TRANSACTION
0.000s SELECT "rooms"."id", "rooms"."name", "rooms"."location", "rooms"."propertyID" FROM "rooms" WHERE ("rooms"."propertyID") = (1) ORDER BY "rooms"."name"
0.000s COMMIT TRANSACTION
0.000s PRAGMA query_only = 0I must be missing something Here is the relevant ItemForm code with comment markers matching my points above. struct ItemForm: View {
@Environment(\.dismiss) var dismiss
@State var item: Item.Draft // 1
@State var currentProperty: Property // 2
@FetchAll var rooms:[Room] //3
@FetchOne var selectedRoom:Room.Draft // 4
@FetchAll(Property.order(by: \.name)) var properties //4
init(item: Item.Draft, currentProperty: Property) {
self._item = State(initialValue: item) // 1
self._currentProperty = State(initialValue: currentProperty)// 2
self._selectedRoom = FetchOne(wrappedValue: Room.Draft(propertyID: currentProperty.id)) //4
_rooms = FetchAll(roomsQuery) // 4
}
var roomsQuery: some SelectStatementOf<Room> { //5
Room
.where{$0.propertyID == currentProperty.id }
.order(by: \.name)
}
@Dependency(\.defaultDatabase) var database
var body: some View {
Form {
....
// 6.1
Picker(selection: $currentProperty) {
ForEach(properties) { property in
Text(property.name).tag(property)
.buttonStyle(.plain)
}
} label: {
HStack {
Image(systemName: "house")
.font(.title)
Text("Property")
}
}
// 6.2
Picker(selection: $item.roomID) {
ForEach(rooms) { room in
Text(room.name)
.tag(room.id)
.buttonStyle(.plain)
}
} label: {
HStack {
Image(systemName: "door.left.hand.open")
.font(.title)
Text("Room")
}
}
}
// 7.1
.task(id: item.roomID) {
await withErrorReporting {
try await $selectedRoom.load()
}
}
// 7.2
.task(id: currentProperty) {
await withErrorReporting {
print("currentProperty.id: \(currentProperty.id, default: "Unknown")")
try await $rooms.load()
}
}
}
|
Replies: 1 comment 1 reply
|
I figured it out. FetchOne is not the solution. I separated the pickers into a separarte subview and it works now as I need. |
I figured it out. FetchOne is not the solution. I separated the pickers into a separarte subview and it works now as I need.