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 an inspiration.
The topics covered in this demo are:
- How to setup 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:
-
GRDBDemoApp
feeds the SwiftUI app with a database, through the SwiftUI environment. -
AppDatabase
is the type that grants database access. It uses DatabaseMigrator in order to setup the database schema, and provides methods that read and write.AppDatabase
is tested. -
This file instantiates various
AppDatabase
for the various projects needs: one database on disk for the application, and in-memory databases for SwiftUI previews. -
Player
is a Record type, able to read and write in the database. It conforms to the standard Codable protocol in order to gain all advantages of Codable Records. -
PlayerListModel
is an@Observable
object that observes the database, displays always fresh values on screen, and performs actions.PlayerListModel
is tested. -
PlayersNavigationView
is the main navigation view of the application. It instantiates aPlayerListModel
from theAppDatabase
stored in the SwiftUI environment.