mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Follow EnvironmentValues.managedObjectContext example: appDatabase is not optional
This commit is contained in:
1 parent
b9e79791a3
commit
b76950f695
12 files changed
+31
-37
No files matched your search
@@ -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 }
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ protocol Queryable: Equatable {
|
||||
/// The property wrapper that observes a database query
|
||||
@propertyWrapper
|
||||
struct Query<Query: Queryable>: 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<Query: Queryable>: 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)
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ struct PlayerEditionView: View {
|
||||
.onDisappear {
|
||||
Task {
|
||||
// save and ignore error
|
||||
try? await appDatabase?.savePlayer(&player)
|
||||
try? await appDatabase.savePlayer(&player)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
|
||||
@@ -24,8 +24,8 @@ protocol Queryable: Equatable {
|
||||
/// The property wrapper that observes a database query
|
||||
@propertyWrapper
|
||||
struct Query<Query: Queryable>: 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<Query: Queryable>: 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)
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -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())
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user