You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Learn how a SwiftUI view can configure the database content it displays.
3
+
Learn how SwiftUI views can configure the database content displayed on screen.
4
4
5
5
## Overview
6
6
7
-
When a SwiftUI view needs to configure the database values it displays, it will modify the ``Queryable`` request that feeds the `@Query` property wrapper.
7
+
When a SwiftUI view needs to configure the database values displayed on screen, it will modify the ``Queryable`` request that feeds the `@Query` property wrapper.
8
+
9
+
Such configuration can be performed by the view that declares a `@Query` property. It can also be performed by the enclosing view. This article explores all your available options.
8
10
9
11
## A Configurable Queryable Type
10
12
11
-
As an example, let's extend the `AllPlayers` request type we have seen in <doc:GettingStarted>. It can now sort players by score, or by name, depending on its `ordering` property.
13
+
As an example, let's extend the `PlayerRequest` request type we have seen in <doc:GettingStarted>. It can now sort players by score, or by name, depending on its `ordering` property.
The `@Query` property wrapper will detect changes in the `ordering` property, and update SwiftUI views accordingly.
48
50
49
-
> Experiment: You can adapt this example for your own needs. As you can see, you can modify the order to database values, but you can also change how they are filtered. All [ValueObservation] features are available.
51
+
> Experiment: You can adapt this example for your own needs. As you can see, you can modify the order to database values, but you can also change how they are filtered. All [GRDB] features are available.
50
52
51
53
## Modifying the Request from the SwiftUI View
52
54
53
-
SwiftUI views can change the properties of the Queryable request with the SwiftUI Binding provided by the `@Query` property wrapper:
55
+
SwiftUI views can change the properties of the Queryable request with the SwiftUI bindings provided by the `@Query` property wrapper:
54
56
55
57
```swift
56
58
importGRDBQuery
57
59
importSwiftUI
58
60
59
61
structPlayerList: View {
60
62
// Ordering can change through the $players.ordering binding.
In the above example, `$players.ordering` is a SwiftUI binding to the `ordering` property of the `PlayerRequest` request.
97
+
98
+
This binding feeds `ToggleOrderingButton`, which lets the user change the ordering of the request. `@Query` then redraws the view with an updated the database content.
99
+
100
+
When appropriate, you can also use `$players.request`, a SwiftUI binding to the `PlayerRequest` request itself.
101
+
102
+
94
103
## Configuring the Initial Request
95
104
96
-
The above example has the `PlayerList` view always start with the `.byScore` ordering. When you want to provide the initial ordering as a parameter to your view, modify the sample code as below:
105
+
The above example has the `PlayerList` view always start with the `.byScore` ordering.
106
+
107
+
When you want to provide the initial request as a parameter to your view, provide a dedicated initializer:
> IMPORTANT: The initial request is only used when `PlayerList` appears on screen. After that, and until `PlayerList` disappears, the request is only controlled by the `$players` bindings described above.
145
+
>
146
+
> This means that calling the `PlayerList(initialOrdering:)` with a different ordering will have no effect:
set: { request in ordering.wrappedValue= request.ordering }))
183
+
}
184
+
185
+
var body: some View { ... }
186
+
}
187
+
```
188
+
189
+
With such a setup, `@Query` updates the database content whenever a change is performed by the `$ordering` Container binding, or the `$players` PlayerList bindings.
190
+
191
+
This is the classic two-way connection enabled by SwiftUI `Binding`.
192
+
193
+
## Initializing @Query from a Constant Request
194
+
195
+
Finally, the ``Query/init(constant:in:)`` initializer allows the enclosing Container view to control the request without restriction, and without any SwiftUI Binding. However, `$players` binding have no effect:
0 commit comments