Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Examples/CloudKitDemo/CloudKitDemoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ import SwiftUI
@main
struct CloudKitDemoApp: App {
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
@Dependency(\.context) var context

init() {
try! prepareDependencies {
try $0.bootstrapDatabase()
if context == .live {
try! prepareDependencies {
try $0.bootstrapDatabase()
}
}
}

var body: some Scene {
WindowGroup {
NavigationStack {
CountersListView()
if context == .live {
NavigationStack {
CountersListView()
}
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion Examples/CloudKitDemo/CountersListFeature.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import CloudKit
import SQLiteData
import SwiftUI
import SwiftUINavigation

struct CountersListView: View {
@FetchAll var counters: [Counter]
Expand Down Expand Up @@ -107,3 +106,12 @@ struct CounterRow: View {
}
}
}

#Preview {
let _ = try! prepareDependencies {
try $0.bootstrapDatabase()
}
NavigationStack {
CountersListView()
}
}
7 changes: 5 additions & 2 deletions Examples/CloudKitDemo/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ nonisolated struct Counter: Identifiable {

extension DependencyValues {
mutating func bootstrapDatabase() throws {
@Dependency(\.context) var context
let database = try SQLiteData.defaultDatabase()
var configuration = Configuration()
configuration.prepareDatabase { db in
try db.attachMetadatabase()
}
let database = try SQLiteData.defaultDatabase(configuration: configuration)
logger.debug(
"""
App database
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Examples/SyncUps/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ extension Int {

extension DependencyValues {
mutating func bootstrapDatabase() throws {
@Dependency(\.context) var context
let database = try SQLiteData.defaultDatabase()
logger.debug(
"""
Expand Down
12 changes: 8 additions & 4 deletions Sources/SQLiteData/CloudKit/SyncEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,11 @@
repeat (each T2).PrimaryKey.QueryOutput: IdentifierStringConvertible,
repeat (each T2).TableColumns.PrimaryColumn: WritableTableColumnExpression
{
@Dependency(\.context) var context
let containerIdentifier =
containerIdentifier
?? ModelConfiguration(groupContainer: .automatic).cloudKitContainerIdentifier

?? (context == .preview ? "preview" : nil)
var allTables: [any SynchronizableTable] = []
var allPrivateTables: [any SynchronizableTable] = []
for table in repeat each tables {
Expand All @@ -113,7 +114,6 @@
}
let userDatabase = UserDatabase(database: database)

@Dependency(\.context) var context
guard context == .live
else {
let privateDatabase = MockCloudDatabase(databaseScope: .private)
Expand Down Expand Up @@ -325,7 +325,10 @@
: URL(filePath: metadatabase.path).lastPathComponent
let attachedMetadatabaseName =
URL(string: attachedMetadatabasePath)?.lastPathComponent ?? ""
if metadatabaseName != attachedMetadatabaseName {
@Dependency(\.context) var context
if metadatabaseName != attachedMetadatabaseName
&& !(context == .preview && attachedMetadatabaseName.isEmpty)
{
throw SchemaError(
reason: .metadatabaseMismatch(
attachedPath: attachedMetadatabasePath,
Expand All @@ -337,7 +340,6 @@
"""
)
}

} else {
try #sql(
"""
Expand Down Expand Up @@ -2151,9 +2153,11 @@
/// - Parameter containerIdentifier: The identifier of the CloudKit container used to
/// synchronize data. Defaults to the value set in the app's entitlements.
public func attachMetadatabase(containerIdentifier: String? = nil) throws {
@Dependency(\.context) var context
let containerIdentifier =
containerIdentifier
?? ModelConfiguration(groupContainer: .automatic).cloudKitContainerIdentifier
?? (context == .preview ? "preview" : nil)

guard let containerIdentifier else {
throw SyncEngine.SchemaError.noCloudKitContainer
Expand Down