-
|
I'm currently working through the documentation to add CloudKit Sync Specifically I'm at the part where it shows how to use SQLite Data in Previews #Preview {
try! prepareDependencies {
try! $0.bootstrapDatabase()
}
// ...
}My preview looks pretty much exactly the same, I've also tried using #Preview {
try! prepareDependencies {
try! $0.bootstrapDatabase()
}
return ContentView()
}For the extension DependencyValues {
mutating func bootstrapDatabase() throws {
defaultDatabase = try appDatabase()
defaultSyncEngine = try SyncEngine(
for: defaultDatabase,
tables: Issue.self, Tag.self, IssueTag.self
)
}
// MARK: - Private
// MARK: Logger
private var dbLogger: Logger { Logger(subsystem: "MyApp", category: "Database") }
// MARK: App Database
func appDatabase() throws -> any DatabaseWriter {
@Dependency(\.context) var context
// MARK: - Database Configuration
var configuration = Configuration()
configuration.foreignKeysEnabled = true
#if DEBUG
configuration.prepareDatabase { db in
try db.attachMetadatabase()
db.trace(options: .profile) { traceEvent in
if context == .preview {
print("\(traceEvent.expandedDescription)")
} else {
dbLogger.debug("\(traceEvent.expandedDescription)")
}
}
}
#endif
// MARK: - Database Creation
let database: any DatabaseWriter
if context == .preview {
database = try DatabaseQueue(configuration: configuration)
} else {
let path = context == .live
? URL.documentsDirectory.appending(component: "db.sqlite").path()
: URL.temporaryDirectory.appending(component: "\(UUID().uuidString)-db.sqlite").path()
dbLogger.info("open \(path)")
database = try DatabasePool(path: path, configuration: configuration)
}
// MARK: - Database Migrations / Creation
var migrator = DatabaseMigrator()
#if DEBUG
migrator.eraseDatabaseOnSchemaChange = true
#endif
migrator.registerMigration("Create tables") { try createTables(in: $0) }
try migrator.migrate(database)
return database
}
// MARK: Migrations
private func createTables(in db: Database) throws { /* ... */ }
}I've added a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Like so often, I discover what's causing the problem right after posting a discussion, even though I spent an hour trying things while writing the discussion post 🙄 I needed to make this change in mutating func bootstrapDatabase() throws {
defaultDatabase = try appDatabase()
if context != .preview { // <-- exclude SyncEngine from previews
defaultSyncEngine = try SyncEngine(
for: defaultDatabase,
tables: Issue.self, Tag.self, IssueTag.self
)
}
}and this one in private func appDatabase() throws -> any DatabaseWriter {
/* ... */
// MARK: - Database Configuration
var configuration = Configuration()
configuration.foreignKeysEnabled = true
#if DEBUG
configuration.prepareDatabase { db in
if context != .preview { <-- don't attach metadatabase for previews
try db.attachMetadatabase()
}
/* ... */
}
#endif
/* ... */
return database
} |
Beta Was this translation helpful? Give feedback.
Like so often, I discover what's causing the problem right after posting a discussion, even though I spent an hour trying things while writing the discussion post 🙄
I needed to make this change in
bootstrapDatabase()and this one in
appDatabase()