Been there, done that: when one feeds a SwiftUI view from an async sequence, the view MUST display an initial empty state, until the Swift Concurrency runtime provides the initial value of the sequence. That's why the demo app uses a Combine publisher for feeding views with database value. This commit removes a misleading reference to async sequences. Fix #1532
3.0 KiB
Async/Await + SwiftUI Demo Application
This demo application is an Async/Await + SwiftUI application. For a demo application that uses UIKit, see GRDBDemoiOS, and for Combine + SwiftUI, see GRDBCombineDemo.
Requirements: iOS 15.0+ / Xcode 13.1+
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 Combine publishers.
- How to apply the recommendations of Recommended Practices for Designing Record Types.
- How to perform
asyncdatabase accesses. - How to feed SwiftUI previews with a transient database.
Files of interest:
-
GRDBAsyncDemoAppfeeds the app views with a database, through the SwiftUI environment. -
AppDatabaseis the type that grants database access. It uses DatabaseMigrator in order to setup the database schema. -
This file instantiates various
AppDatabasefor the various projects needs: 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 advantages of Codable Records. -
PlayerRequest.swift, AppView.swift
PlayerRequestdefines the player requests used by the app (sorted by score, or by name).PlayerRequestfeeds the@Queryproperty wrapper (@Query, defined in GRDBQuery, allows SwiftUI views to display up-to-date database content).AppViewis the SwiftUI view that uses@Queryin order to feed its player list. -
- Test the database schema
- Test the
Playerrecord and its requests - Test the
PlayerRequestmethods that feed the list of players. - Test the
AppDatabasemethods that let the app access the database.