GRDBDemo Application
GRDBDemo demonstrates how GRDB can fuel a SwiftUI application.
Note
: This demo app is not a project template. Do not copy it as a starting point for your application. Instead, create a new project, choose a GRDB installation method, and use the demo as inspiration.
The topics covered in this demo are:
- How to set up a database in an iOS app.
- How to define a simple Codable Record.
- How to track database changes and animate a SwiftUI List with ValueObservation.
- How to apply the recommendations of Recommended Practices for Designing Record Types.
- How to feed SwiftUI previews with a transient database.
Files of interest:
-
GRDBDemoAppfeeds the SwiftUI app with a database, through the SwiftUI environment. -
AppDatabaseis the type that grants database access. It uses DatabaseMigrator in order to set up the database schema, and provides methods that read and write.AppDatabaseis tested. -
This file instantiates
AppDatabaseinstances for the various needs of the project: one database on disk for the application, and in-memory databases for SwiftUI previews. -
Playeris a Record type, able to read and write in the database. It conforms to the standard Codable protocol in order to gain all the advantages of Codable Records. -
PlayerListModelis an@Observableobject that observes the database, always displays fresh values on screen, and performs actions.PlayerListModelis tested. -
PlayersNavigationViewis the main navigation view of the application. It instantiates aPlayerListModelfrom theAppDatabasestored in the SwiftUI environment.