Combine + SwiftUI Demo Application
This demo application is a Combine + SwiftUI application, based on the MVVM design pattern. For a demo application that uses UIKit, see GRDBDemoiOS.
☝️ 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 Good Practices for Designing Record Types.
- How to feed SwiftUI previews with a transient database.
Files of interest:
-
GRDBCombineDemoAppfeeds the app views with a database. -
AppDatabaseis the type that grants database access. It uses DatabaseMigrator in order to setup the database schema, and ValueObservation in order to let the application observe database changes. -
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. It defines the database requests used by the application. -
PlayerList.swift and PlayerListViewModel.swift
PlayerListis the SwiftUI view that displays the list of players, fed byPlayerListViewModel. -
PlayerForm.swift, PlayerEditionView.swift, PlayerCreationSheet.swift and PlayerFormViewModel.swift.
PlayerFormis the SwiftUI view that displays a Player editing form. It is embedded inPlayerEditionViewandPlayerCreationSheet, two SwiftUI views that edit or create a player. All those views are fed byPlayerFormViewModel. -
- Test the database schema
- Test the
Playerrecord and its requests - Test the
AppDatabasemethods that let the app access the database.