Use the @Query property wrapper to observe database records
mainThe @Query property wrapper allows SwiftUI views to directly read and observe database records. It automatically updates the view whenever the underlying database changes. You provide a request object (conforming to the necessary protocols) to the wrapper, and it manages the lifecycle of the database observation.
/// Displays an always up-to-date list of database players.
struct PlayerList: View {
@Query(PlayersRequest()) var players: [Player]
var body: some View {
List(players) { player in Text(player.name) }
}
}