Skip to content

Commit 547a508

Browse files
authored
Throw error when sharing while sync engine is stopped. (#234)
* Throw error when sharing while sync engine is stopped. * snapshot
1 parent fa43586 commit 547a508

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

Sources/SQLiteData/CloudKit/CloudKitSharing.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@
3535
case recordNotRoot([ForeignKey])
3636
case recordTableNotSynchronized
3737
case recordTablePrivate
38+
case syncEngineNotRunning
3839
}
3940

40-
let recordTableName: String
41-
let recordPrimaryKey: String
41+
var recordTableName: String?
42+
var recordPrimaryKey: String?
4243
let reason: Reason
4344
let debugDescription: String
4445

@@ -71,6 +72,16 @@
7172
configure: @Sendable (CKShare) -> Void
7273
) async throws -> SharedRecord
7374
where T.TableColumns.PrimaryKey.QueryOutput: IdentifierStringConvertible {
75+
guard isRunning
76+
else {
77+
throw SharingError(
78+
reason: .syncEngineNotRunning,
79+
debugDescription: """
80+
Sync engine is not running. Make sure engine is running by invoking the 'start()' \
81+
method, or using the 'startImmediately' argument when initializing the engine.
82+
"""
83+
)
84+
}
7485
guard tablesByName[T.tableName] != nil
7586
else {
7687
throw SharingError(

Tests/SQLiteDataTests/CloudKitTests/SharingTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,35 @@
5555
}
5656
}
5757

58+
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
59+
@Test func syncEngineStopped() async throws {
60+
let remindersList = RemindersList(id: 1, title: "Personal")
61+
try await userDatabase.userWrite { db in
62+
try db.seed { remindersList }
63+
}
64+
try await syncEngine.processPendingRecordZoneChanges(scope: .private)
65+
syncEngine.stop()
66+
67+
let error = await #expect(throws: (any Error).self) {
68+
_ = try await self.syncEngine.share(record: remindersList, configure: { _ in })
69+
}
70+
assertInlineSnapshot(of: error?.localizedDescription, as: .customDump) {
71+
"""
72+
"The record could not be shared."
73+
"""
74+
}
75+
assertInlineSnapshot(of: error, as: .customDump) {
76+
#"""
77+
SyncEngine.SharingError(
78+
recordTableName: nil,
79+
recordPrimaryKey: nil,
80+
reason: .syncEngineNotRunning,
81+
debugDescription: "Sync engine is not running. Make sure engine is running by invoking the \'start()\' method, or using the \'startImmediately\' argument when initializing the engine."
82+
)
83+
"""#
84+
}
85+
}
86+
5887
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
5988
@Test func shareUnrecognizedTable() async throws {
6089
let error = await #expect(throws: (any Error).self) {

Tests/SQLiteDataTests/Internal/BaseCloudKitTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ class BaseCloudKitTests: @unchecked Sendable {
124124

125125
deinit {
126126
if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) {
127+
guard syncEngine.isRunning
128+
else { return }
129+
127130
syncEngine.shared.assertFetchChangesScopes([])
128131
syncEngine.shared.state.assertPendingDatabaseChanges([])
129132
syncEngine.shared.state.assertPendingRecordZoneChanges([])
@@ -132,6 +135,7 @@ class BaseCloudKitTests: @unchecked Sendable {
132135
syncEngine.private.state.assertPendingDatabaseChanges([])
133136
syncEngine.private.state.assertPendingRecordZoneChanges([])
134137
syncEngine.private.assertAcceptedShareMetadata([])
138+
135139
try! syncEngine.metadatabase.read { db in
136140
try #expect(UnsyncedRecordID.count().fetchOne(db) == 0)
137141
}

0 commit comments

Comments
 (0)