diff --git a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/GRDBAsyncDemoApp.swift b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/GRDBAsyncDemoApp.swift index 843c6497c..d8f66cbf2 100644 --- a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/GRDBAsyncDemoApp.swift +++ b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/GRDBAsyncDemoApp.swift @@ -5,18 +5,19 @@ import SwiftUI struct GRDBAsyncDemoApp: App { var body: some Scene { WindowGroup { - AppView().environment(\.appDatabase, AppDatabase.shared) + AppView().environment(\.appDatabase, .shared) } } } -// Let SwiftUI views access the database through the SwiftUI environment +// Let SwiftUI views access the app database through the SwiftUI environment private struct AppDatabaseKey: EnvironmentKey { - static let defaultValue: AppDatabase? = nil + /// Default appDatabase is an empty in-memory database + static var defaultValue: AppDatabase { .empty() } } extension EnvironmentValues { - var appDatabase: AppDatabase? { + var appDatabase: AppDatabase { get { self[AppDatabaseKey.self] } set { self[AppDatabaseKey.self] = newValue } } diff --git a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Query.swift b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Query.swift index 0b7b4a746..f6b171395 100644 --- a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Query.swift +++ b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Query.swift @@ -26,8 +26,8 @@ protocol Queryable: Equatable { /// The property wrapper that observes a database query @propertyWrapper struct Query: DynamicProperty { - /// The AppDatabase that grants access to the database - @Environment(\.appDatabase) private var appDatabase: AppDatabase? + /// Database access + @Environment(\.appDatabase) private var appDatabase @StateObject private var core = Core() private var baseQuery: Query @@ -53,9 +53,6 @@ struct Query: DynamicProperty { } func update() { - guard let appDatabase = appDatabase else { - fatalError("Attempting to use @Query without any database in the environment") - } // Feed core with necessary information, and make sure tracking has started if core.usesBaseQuery { core.query = baseQuery } core.startTrackingIfNecessary(in: appDatabase) diff --git a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/AppView.swift b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/AppView.swift index c089b3bb3..c5a6b4096 100644 --- a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/AppView.swift +++ b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/AppView.swift @@ -68,7 +68,7 @@ struct AppView: View { // Don't stopEditing() here because this is // performed `onChange(of: players)` Task { - try? await appDatabase?.deleteAllPlayers() + try? await appDatabase.deleteAllPlayers() } } label: { Image(systemName: "trash").imageScale(.large) @@ -79,7 +79,7 @@ struct AppView: View { Button { stopEditing() Task { - try? await appDatabase?.refreshPlayers() + try? await appDatabase.refreshPlayers() } } label: { Image(systemName: "arrow.clockwise").imageScale(.large) @@ -94,7 +94,7 @@ struct AppView: View { try? await withThrowingTaskGroup(of: Void.self) { group in for _ in 0..<50 { _ = group.addTaskUnlessCancelled { - try await appDatabase?.refreshPlayers() + try await appDatabase.refreshPlayers() } } try await group.waitForAll() @@ -154,11 +154,11 @@ private struct ToggleOrderingButton: View { struct AppView_Previews: PreviewProvider { static var previews: some View { Group { + // Preview the default, empty database + AppView() + // Preview a database of random players AppView().environment(\.appDatabase, .random()) - - // Preview an empty database - AppView().environment(\.appDatabase, .empty()) } } } diff --git a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/PlayerCreationView.swift b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/PlayerCreationView.swift index b7f9c5c89..74919ec0d 100644 --- a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/PlayerCreationView.swift +++ b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/PlayerCreationView.swift @@ -33,7 +33,7 @@ struct PlayerCreationView: View { private func save() async { do { var player = Player(id: nil, name: name, score: Int(score) ?? 0) - try await appDatabase?.savePlayer(&player) + try await appDatabase.savePlayer(&player) dismiss() } catch { errorAlertTitle = (error as? LocalizedError)?.errorDescription ?? "An error occurred" @@ -45,6 +45,5 @@ struct PlayerCreationView: View { struct PlayerCreationSheet_Previews: PreviewProvider { static var previews: some View { PlayerCreationView() - .environment(\.appDatabase, .empty()) } } diff --git a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/PlayerEditionView.swift b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/PlayerEditionView.swift index 79f8cdaa0..02680f926 100644 --- a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/PlayerEditionView.swift +++ b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/PlayerEditionView.swift @@ -14,7 +14,7 @@ struct PlayerEditionView: View { .onDisappear { Task { // save and ignore error - try? await appDatabase?.savePlayer(&player) + try? await appDatabase.savePlayer(&player) } } } diff --git a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/PlayerList.swift b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/PlayerList.swift index ceb62b13c..ea959e1b0 100644 --- a/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/PlayerList.swift +++ b/Documentation/DemoApps/GRDBAsyncDemo/GRDBAsyncDemo/Views/PlayerList.swift @@ -19,7 +19,7 @@ struct PlayerList: View { .onDelete { offsets in let playerIds = offsets.compactMap { players[$0].id } Task { - try? await appDatabase?.deletePlayers(ids: playerIds) + try? await appDatabase.deletePlayers(ids: playerIds) } } } diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/GRDBCombineDemoApp.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/GRDBCombineDemoApp.swift index aef733ade..dde184be0 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/GRDBCombineDemoApp.swift +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/GRDBCombineDemoApp.swift @@ -5,18 +5,19 @@ import SwiftUI struct GRDBCombineDemoApp: App { var body: some Scene { WindowGroup { - AppView().environment(\.appDatabase, AppDatabase.shared) + AppView().environment(\.appDatabase, .shared) } } } -// Let SwiftUI views access the database through the SwiftUI environment +// Let SwiftUI views access the app database through the SwiftUI environment private struct AppDatabaseKey: EnvironmentKey { - static let defaultValue: AppDatabase? = nil + /// Default appDatabase is an empty in-memory database + static var defaultValue: AppDatabase { .empty() } } extension EnvironmentValues { - var appDatabase: AppDatabase? { + var appDatabase: AppDatabase { get { self[AppDatabaseKey.self] } set { self[AppDatabaseKey.self] = newValue } } diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Query.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Query.swift index 960e12311..d08bf0d07 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Query.swift +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Query.swift @@ -24,8 +24,8 @@ protocol Queryable: Equatable { /// The property wrapper that observes a database query @propertyWrapper struct Query: DynamicProperty { - /// The AppDatabase that grants access to the database - @Environment(\.appDatabase) private var appDatabase: AppDatabase? + /// Database access + @Environment(\.appDatabase) private var appDatabase @StateObject private var core = Core() private var baseQuery: Query @@ -51,9 +51,6 @@ struct Query: DynamicProperty { } func update() { - guard let appDatabase = appDatabase else { - fatalError("Attempting to use @Query without any database in the environment") - } // Feed core with necessary information, and make sure tracking has started if core.usesBaseQuery { core.query = baseQuery } core.startTrackingIfNecessary(in: appDatabase) diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/AppView.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/AppView.swift index c414e001e..df2ffbdde 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/AppView.swift +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/AppView.swift @@ -36,11 +36,11 @@ struct AppView: View { private var toolbarContent: some ToolbarContent { ToolbarItemGroup(placement: .bottomBar) { Button( - action: { try? appDatabase?.deleteAllPlayers() }, + action: { try? appDatabase.deleteAllPlayers() }, label: { Image(systemName: "trash").imageScale(.large) }) Spacer() Button( - action: { try? appDatabase?.refreshPlayers() }, + action: { try? appDatabase.refreshPlayers() }, label: { Image(systemName: "arrow.clockwise").imageScale(.large) }) } } @@ -91,11 +91,11 @@ private struct ToggleOrderingButton: View { struct AppView_Previews: PreviewProvider { static var previews: some View { Group { + // Preview the default, empty database + AppView() + // Preview a database of random players AppView().environment(\.appDatabase, .random()) - - // Preview an empty database - AppView().environment(\.appDatabase, .empty()) } } } diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerCreationView.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerCreationView.swift index 122e0811d..b487d336e 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerCreationView.swift +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerCreationView.swift @@ -31,7 +31,7 @@ struct PlayerCreationView: View { private func save() { do { var player = Player(id: nil, name: name, score: Int(score) ?? 0) - try appDatabase?.savePlayer(&player) + try appDatabase.savePlayer(&player) dismissAction() } catch { errorAlertTitle = (error as? LocalizedError)?.errorDescription ?? "An error occurred" @@ -43,6 +43,5 @@ struct PlayerCreationView: View { struct PlayerCreationSheet_Previews: PreviewProvider { static var previews: some View { PlayerCreationView(dismissAction: { }) - .environment(\.appDatabase, .empty()) } } diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerEditionView.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerEditionView.swift index eff839e2f..eae08c6f5 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerEditionView.swift +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerEditionView.swift @@ -13,7 +13,7 @@ struct PlayerEditionView: View { set: { player.score = Int($0) ?? 0 })) .onDisappear { // save and ignore error - try? appDatabase?.savePlayer(&player) + try? appDatabase.savePlayer(&player) } } } diff --git a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerList.swift b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerList.swift index 76dbe3783..d6acbc553 100644 --- a/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerList.swift +++ b/Documentation/DemoApps/GRDBCombineDemo/GRDBCombineDemo/Views/PlayerList.swift @@ -16,7 +16,7 @@ struct PlayerList: View { } .onDelete(perform: { offsets in let playerIds = offsets.compactMap { players[$0].id } - try? appDatabase?.deletePlayers(ids: playerIds) + try? appDatabase.deletePlayers(ids: playerIds) }) } .animation(.default)