Skip to content

Commit 5a05b22

Browse files
authored
Fix previews for CloudKit-enabled apps (#366)
* wip * wip * wip * wip * wip * Conditionally wrap CountersListView in NavigationStack
1 parent 8af16d5 commit 5a05b22

6 files changed

Lines changed: 32 additions & 31 deletions

File tree

Examples/CloudKitDemo/CloudKitDemoApp.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@ import SwiftUI
55
@main
66
struct CloudKitDemoApp: App {
77
@UIApplicationDelegateAdaptor private var appDelegate: AppDelegate
8+
@Dependency(\.context) var context
89

910
init() {
10-
try! prepareDependencies {
11-
try $0.bootstrapDatabase()
11+
if context == .live {
12+
try! prepareDependencies {
13+
try $0.bootstrapDatabase()
14+
}
1215
}
1316
}
1417

1518
var body: some Scene {
1619
WindowGroup {
17-
NavigationStack {
18-
CountersListView()
20+
if context == .live {
21+
NavigationStack {
22+
CountersListView()
23+
}
1924
}
2025
}
2126
}

Examples/CloudKitDemo/CountersListFeature.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import CloudKit
22
import SQLiteData
33
import SwiftUI
4-
import SwiftUINavigation
54

65
struct CountersListView: View {
76
@FetchAll var counters: [Counter]
@@ -107,3 +106,12 @@ struct CounterRow: View {
107106
}
108107
}
109108
}
109+
110+
#Preview {
111+
let _ = try! prepareDependencies {
112+
try $0.bootstrapDatabase()
113+
}
114+
NavigationStack {
115+
CountersListView()
116+
}
117+
}

Examples/CloudKitDemo/Schema.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ nonisolated struct Counter: Identifiable {
1010

1111
extension DependencyValues {
1212
mutating func bootstrapDatabase() throws {
13-
@Dependency(\.context) var context
14-
let database = try SQLiteData.defaultDatabase()
13+
var configuration = Configuration()
14+
configuration.prepareDatabase { db in
15+
try db.attachMetadatabase()
16+
}
17+
let database = try SQLiteData.defaultDatabase(configuration: configuration)
1518
logger.debug(
1619
"""
1720
App database

Examples/Examples.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 1 addition & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/SyncUps/Schema.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ extension Int {
7777

7878
extension DependencyValues {
7979
mutating func bootstrapDatabase() throws {
80-
@Dependency(\.context) var context
8180
let database = try SQLiteData.defaultDatabase()
8281
logger.debug(
8382
"""

Sources/SQLiteData/CloudKit/SyncEngine.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@
9999
repeat (each T2).PrimaryKey.QueryOutput: IdentifierStringConvertible,
100100
repeat (each T2).TableColumns.PrimaryColumn: WritableTableColumnExpression
101101
{
102+
@Dependency(\.context) var context
102103
let containerIdentifier =
103104
containerIdentifier
104105
?? ModelConfiguration(groupContainer: .automatic).cloudKitContainerIdentifier
105-
106+
?? (context == .preview ? "preview" : nil)
106107
var allTables: [any SynchronizableTable] = []
107108
var allPrivateTables: [any SynchronizableTable] = []
108109
for table in repeat each tables {
@@ -113,7 +114,6 @@
113114
}
114115
let userDatabase = UserDatabase(database: database)
115116

116-
@Dependency(\.context) var context
117117
guard context == .live
118118
else {
119119
let privateDatabase = MockCloudDatabase(databaseScope: .private)
@@ -325,7 +325,10 @@
325325
: URL(filePath: metadatabase.path).lastPathComponent
326326
let attachedMetadatabaseName =
327327
URL(string: attachedMetadatabasePath)?.lastPathComponent ?? ""
328-
if metadatabaseName != attachedMetadatabaseName {
328+
@Dependency(\.context) var context
329+
if metadatabaseName != attachedMetadatabaseName
330+
&& !(context == .preview && attachedMetadatabaseName.isEmpty)
331+
{
329332
throw SchemaError(
330333
reason: .metadatabaseMismatch(
331334
attachedPath: attachedMetadatabasePath,
@@ -337,7 +340,6 @@
337340
"""
338341
)
339342
}
340-
341343
} else {
342344
try #sql(
343345
"""
@@ -2151,9 +2153,11 @@
21512153
/// - Parameter containerIdentifier: The identifier of the CloudKit container used to
21522154
/// synchronize data. Defaults to the value set in the app's entitlements.
21532155
public func attachMetadatabase(containerIdentifier: String? = nil) throws {
2156+
@Dependency(\.context) var context
21542157
let containerIdentifier =
21552158
containerIdentifier
21562159
?? ModelConfiguration(groupContainer: .automatic).cloudKitContainerIdentifier
2160+
?? (context == .preview ? "preview" : nil)
21572161

21582162
guard let containerIdentifier else {
21592163
throw SyncEngine.SchemaError.noCloudKitContainer

0 commit comments

Comments
 (0)