Skip to content

Commit 84255ac

Browse files
committed
wip
1 parent 82490a5 commit 84255ac

4 files changed

Lines changed: 59 additions & 32 deletions

File tree

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#if canImport(CloudKit)
2-
import CloudKit
3-
import Dependencies
4-
import GRDB
2+
import CloudKit
3+
import Dependencies
4+
import GRDB
55

6-
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
7-
extension DependencyValues {
8-
public var defaultSyncEngine: SyncEngine {
9-
get { self[SyncEngine.self] }
10-
set { self[SyncEngine.self] = newValue }
6+
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
7+
extension DependencyValues {
8+
public var defaultSyncEngine: SyncEngine {
9+
get { self[SyncEngine.self] }
10+
set { self[SyncEngine.self] = newValue }
11+
}
1112
}
12-
}
1313

14-
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
15-
extension SyncEngine: TestDependencyKey {
16-
public static var testValue: SyncEngine {
17-
try! SyncEngine(container: .default(), database: DatabaseQueue(), tables: [])
14+
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
15+
extension SyncEngine: TestDependencyKey {
16+
public static var testValue: SyncEngine {
17+
try! SyncEngine(for: DatabaseQueue())
18+
}
1819
}
19-
}
2020
#endif

Sources/SharingGRDBCore/CloudKit/SyncEngine.swift

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import CustomDump
55
import OrderedCollections
66
import OSLog
7+
import StructuredQueriesCore
8+
import SwiftData
79

810
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
911
public final class SyncEngine: Sendable {
@@ -24,14 +26,41 @@
2426

2527
let dataManager = Dependency(\.dataManager)
2628

27-
public convenience init(
28-
container: CKContainer,
29+
public convenience init<each T1: PrimaryKeyedTable, each T2: PrimaryKeyedTable>(
30+
for database: any DatabaseWriter,
31+
tables: repeat (each T1).Type,
32+
privateTables: repeat (each T2).Type,
33+
containerIdentifier: String? = nil,
2934
defaultZone: CKRecordZone = CKRecordZone(zoneName: "co.pointfree.SQLiteData.defaultZone"),
30-
database: any DatabaseWriter,
31-
logger: Logger = Logger(subsystem: "SQLiteData", category: "CloudKit"),
32-
tables: [any PrimaryKeyedTable.Type],
33-
privateTables: [any PrimaryKeyedTable.Type] = []
34-
) throws {
35+
logger: Logger = Logger(subsystem: "SQLiteData", category: "CloudKit")
36+
) throws
37+
where
38+
repeat (each T1).PrimaryKey.QueryOutput: IdentifierStringConvertible,
39+
repeat (each T2).PrimaryKey.QueryOutput: IdentifierStringConvertible
40+
{
41+
let containerIdentifier = containerIdentifier
42+
?? ModelConfiguration(groupContainer: .automatic).cloudKitContainerIdentifier
43+
44+
guard let containerIdentifier else {
45+
throw SchemaError(
46+
reason: .noCloudKitContainer,
47+
debugDescription: """
48+
No default CloudKit container found. Please add a container identifier to your app's \
49+
entitlements.
50+
"""
51+
)
52+
}
53+
54+
let container = CKContainer(identifier: containerIdentifier)
55+
var allTables: [any PrimaryKeyedTable.Type] = []
56+
var allPrivateTables: [any PrimaryKeyedTable.Type] = []
57+
for table in repeat each tables {
58+
allTables.append(table)
59+
}
60+
for privateTable in repeat each privateTables {
61+
allPrivateTables.append(privateTable)
62+
}
63+
3564
let userDatabase = UserDatabase(database: database)
3665
try self.init(
3766
container: container,
@@ -66,8 +95,8 @@
6695
},
6796
userDatabase: userDatabase,
6897
logger: logger,
69-
tables: tables,
70-
privateTables: privateTables
98+
tables: allTables,
99+
privateTables: allPrivateTables
71100
)
72101
_ = try setUpSyncEngine(
73102
userDatabase: userDatabase,
@@ -1493,7 +1522,8 @@
14931522
///
14941523
/// - Parameter containerIdentifier: The identifier of the CloudKit container used to synchronize
14951524
/// data.
1496-
public func attachMetadatabase(containerIdentifier: String) throws {
1525+
public func attachMetadatabase(containerIdentifier: String? = nil) throws {
1526+
// TODO
14971527
let databasePath = try SQLQueryExpression(
14981528
"""
14991529
SELECT "file" FROM pragma_database_list()
@@ -1539,6 +1569,7 @@
15391569
case invalidForeignKeyAction(ForeignKey)
15401570
case invalidTableName(String)
15411571
case metadatabaseMismatch(attachedPath: String, syncEngineConfiguredPath: String)
1572+
case noCloudKitContainer
15421573
case nonNullColumnsWithoutDefault(tableName: String, columnNames: [String])
15431574
case triggersWithoutSynchronizationCheck([String])
15441575
case unknown

Sources/SharingGRDBCore/Documentation.docc/Articles/CloudKit.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,11 @@ struct MyApp: App {
8989
```
9090

9191
The `SyncEngine`
92-
[initializer](<doc:SyncEngine/init(container:defaultZone:database:logger:tables:privateTables:)>)
92+
[initializer](<doc:SyncEngine/init(for:tables:privateTables:containerIdentifier:defaultZone:logger:)>)
9393
has more options you may be interested in configuring.
9494

95-
> Important: A few important things to note about this:
96-
>
97-
> * The CloudKit container identifier must be explicitly provided and unfortunately cannot be
98-
> extracted from Entitlements.plist automatically. That privilege is only afforded to SwiftData.
99-
> * You must explicitly provide all tables that you want to synchronize. We do this so that you
100-
> can have the option of having some local tables that are not synchronized to CloudKit.
95+
> Important: You must explicitly provide all tables that you want to synchronize. We do this so that
96+
> you can have the option of having some local tables that are not synchronized to CloudKit.
10197
10298
Once this work is done the app should work exactly as it did before, but now any changes made
10399
to the database will be synchronized to CloudKit. You will still interact with your local SQLite

Sources/SharingGRDBCore/Documentation.docc/Articles/CloudKitSharing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ struct MyApp: App {
391391
try! prepareDependencies {
392392
$0.defaultDatabase = try appDatabase()
393393
$0.defaultSyncEngine = try SyncEngine(
394-
database: $0.defaultDatabase,
394+
for: $0.defaultDatabase,
395395
tables: RemindersList.self, Reminder.self,
396396
privateTables: RemindersListPrivate.self
397397
)

0 commit comments

Comments
 (0)