diff --git a/Examples/CloudKitDemo/CloudKitDemoApp.swift b/Examples/CloudKitDemo/CloudKitDemoApp.swift index aef2e449..d02d1d73 100644 --- a/Examples/CloudKitDemo/CloudKitDemoApp.swift +++ b/Examples/CloudKitDemo/CloudKitDemoApp.swift @@ -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() + } } } } diff --git a/Examples/CloudKitDemo/CountersListFeature.swift b/Examples/CloudKitDemo/CountersListFeature.swift index 98b904e2..1a1a5c6f 100644 --- a/Examples/CloudKitDemo/CountersListFeature.swift +++ b/Examples/CloudKitDemo/CountersListFeature.swift @@ -1,7 +1,6 @@ import CloudKit import SQLiteData import SwiftUI -import SwiftUINavigation struct CountersListView: View { @FetchAll var counters: [Counter] @@ -107,3 +106,12 @@ struct CounterRow: View { } } } + +#Preview { + let _ = try! prepareDependencies { + try $0.bootstrapDatabase() + } + NavigationStack { + CountersListView() + } +} diff --git a/Examples/CloudKitDemo/Schema.swift b/Examples/CloudKitDemo/Schema.swift index 4721e583..03eaf63c 100644 --- a/Examples/CloudKitDemo/Schema.swift +++ b/Examples/CloudKitDemo/Schema.swift @@ -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 diff --git a/Examples/Examples.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Examples/Examples.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 4ff76b6f..1f540899 100644 --- a/Examples/Examples.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Examples/Examples.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "41e7781e6c506773b6af84af513bcd6d3b1be59d635e6c4c4bd89638368e4629", + "originHash" : "c133bf7d10c8ce1e5d6506c3d2f080eac8b4c8c2827044d53a9b925e903564fd", "pins" : [ { "identity" : "combine-schedulers", @@ -73,24 +73,6 @@ "version" : "1.10.0" } }, - { - "identity" : "swift-docc-plugin", - "kind" : "remoteSourceControl", - "location" : "https://github.com/apple/swift-docc-plugin", - "state" : { - "revision" : "3e4f133a77e644a5812911a0513aeb7288b07d06", - "version" : "1.4.5" - } - }, - { - "identity" : "swift-docc-symbolkit", - "kind" : "remoteSourceControl", - "location" : "https://github.com/swiftlang/swift-docc-symbolkit", - "state" : { - "revision" : "b45d1f2ed151d057b54504d653e0da5552844e34", - "version" : "1.0.0" - } - }, { "identity" : "swift-identified-collections", "kind" : "remoteSourceControl", diff --git a/Examples/SyncUps/Schema.swift b/Examples/SyncUps/Schema.swift index 9ac636d2..a3063f5b 100644 --- a/Examples/SyncUps/Schema.swift +++ b/Examples/SyncUps/Schema.swift @@ -77,7 +77,6 @@ extension Int { extension DependencyValues { mutating func bootstrapDatabase() throws { - @Dependency(\.context) var context let database = try SQLiteData.defaultDatabase() logger.debug( """ diff --git a/Sources/SQLiteData/CloudKit/SyncEngine.swift b/Sources/SQLiteData/CloudKit/SyncEngine.swift index 5d094197..24891aa4 100644 --- a/Sources/SQLiteData/CloudKit/SyncEngine.swift +++ b/Sources/SQLiteData/CloudKit/SyncEngine.swift @@ -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 { @@ -113,7 +114,6 @@ } let userDatabase = UserDatabase(database: database) - @Dependency(\.context) var context guard context == .live else { let privateDatabase = MockCloudDatabase(databaseScope: .private) @@ -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, @@ -337,7 +340,6 @@ """ ) } - } else { try #sql( """ @@ -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