Skip to content

Commit 12b8b4e

Browse files
authored
Expose SyncEngine.isSynchronizing as Swift/database function pair (#372)
* Expose SyncEngine.isSynchronizing as Swift/database function pair This deprecates the existing query expression API for a `@DatabaseFunction` with a projected query expression: ```swift // Before: SyncEngine.isSynchronizingChanges() // some QueryExpression<Bool> // After: SyncEngine.isSynchronizing // Bool SyncEngine.$isSynchronizing // some QueryExpression<Bool> ``` * wip
1 parent b373f0c commit 12b8b4e

File tree

9 files changed

+33
-33
lines changed

9 files changed

+33
-33
lines changed

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

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

Package.resolved

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

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let package = Package(
3737
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.18.4"),
3838
.package(
3939
url: "https://github.com/pointfreeco/swift-structured-queries",
40-
from: "0.24.0",
40+
from: "0.27.0",
4141
traits: [
4242
.trait(name: "StructuredQueriesTagged", condition: .when(traits: ["SQLiteDataTagged"]))
4343
]

[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let package = Package(
2828
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.9.0"),
2929
.package(url: "https://github.com/pointfreeco/swift-sharing", from: "2.3.0"),
3030
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.18.4"),
31-
.package(url: "https://github.com/pointfreeco/swift-structured-queries", from: "0.24.0"),
31+
.package(url: "https://github.com/pointfreeco/swift-structured-queries", from: "0.27.0"),
3232
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.5.0"),
3333
],
3434
targets: [

Sources/SQLiteData/CloudKit/Internal/CloudKitFunctions.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,4 @@
1818
return share.publicPermission == .readWrite
1919
|| share.currentUserParticipant?.permission == .readWrite
2020
}
21-
22-
@DatabaseFunction("sqlitedata_icloud_syncEngineIsSynchronizingChanges")
23-
func syncEngineIsSynchronizingChanges() -> Bool {
24-
_isSynchronizingChanges
25-
}
2621
#endif

Sources/SQLiteData/CloudKit/Internal/Triggers.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
}
142142
.update { $0._isDeleted = true }
143143
} when: { _ in
144-
!SyncEngine.isSynchronizingChanges()
144+
!SyncEngine.$isSynchronizing
145145
}
146146
)
147147
}
@@ -158,7 +158,7 @@
158158
}
159159
.delete()
160160
} when: { _ in
161-
SyncEngine.isSynchronizingChanges()
161+
SyncEngine.$isSynchronizing
162162
}
163163
)
164164
}
@@ -263,7 +263,7 @@
263263
)
264264
)
265265
} when: { _ in
266-
!SyncEngine.isSynchronizingChanges()
266+
!SyncEngine.$isSynchronizing
267267
}
268268
)
269269
}
@@ -323,7 +323,7 @@
323323
)
324324
)
325325
} when: { old, new in
326-
old._isDeleted.eq(new._isDeleted) && !SyncEngine.isSynchronizingChanges()
326+
old._isDeleted.eq(new._isDeleted) && !SyncEngine.$isSynchronizing
327327
}
328328
)
329329
}
@@ -344,7 +344,7 @@
344344
)
345345
)
346346
} when: { old, new in
347-
!old._isDeleted && new._isDeleted && !SyncEngine.isSynchronizingChanges()
347+
!old._isDeleted && new._isDeleted && !SyncEngine.$isSynchronizing
348348
}
349349
)
350350
}
@@ -453,7 +453,7 @@
453453
)
454454
}
455455
.where {
456-
!SyncEngine.isSynchronizingChanges()
456+
!SyncEngine.$isSynchronizing
457457
&& $0.parentRecordName.is(nil)
458458
&& !$hasPermission($0.share)
459459
}

Sources/SQLiteData/CloudKit/SyncEngine.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@
349349
.execute(db)
350350
}
351351
db.add(function: $currentTime)
352-
db.add(function: $syncEngineIsSynchronizingChanges)
352+
db.add(function: SyncEngine.$isSynchronizing)
353353
db.add(function: $didUpdate)
354354
db.add(function: $didDelete)
355355
db.add(function: $hasPermission)
@@ -864,12 +864,17 @@
864864
)
865865
}
866866

867-
/// A query expression that can be used in SQL queries to determine if the ``SyncEngine``
868-
/// is currently writing changes to the database.
867+
/// Whether or not the ``SyncEngine`` is currently writing changes to the database.
869868
///
870869
/// See <doc:CloudKit#Updating-triggers-to-be-compatible-with-synchronization> for more info.
870+
@DatabaseFunction("sqlitedata_icloud_syncEngineIsSynchronizingChanges")
871+
public static var isSynchronizing: Bool {
872+
_isSynchronizingChanges
873+
}
874+
875+
@available(*, deprecated, message: "Use 'SyncEngine.$isSynchronizing', instead.")
871876
public static func isSynchronizingChanges() -> some QueryExpression<Bool> {
872-
$syncEngineIsSynchronizingChanges()
877+
$isSynchronizing
873878
}
874879

875880
private var sendingChangesCount: Int {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ or CloudKit is updating the data.
749749
750750
[FTS]: https://sqlite.org/fts5.html
751751
752-
To customize this behavior you can use the ``SyncEngine/isSynchronizingChanges()`` SQL expression.
752+
To customize this behavior you can use the projected ``SyncEngine/isSynchronizing`` SQL expression.
753753
It represents a custom database function that is installed in your database connection, and it will
754754
return true if the write to your database originates from the sync engine. You can use it in a
755755
trigger like so:
@@ -759,7 +759,7 @@ trigger like so:
759759
"""
760760
CREATE TEMPORARY TRIGGER "…"
761761
AFTER DELETE ON "…"
762-
FOR EACH ROW WHEN NOT \(SyncEngine.isSynchronizingChanges())
762+
FOR EACH ROW WHEN NOT \(SyncEngine.$isSynchronizing)
763763
BEGIN
764764
765765
END
@@ -776,7 +776,7 @@ Model.createTemporaryTrigger(
776776
after: .insert { new in
777777
// ...
778778
} when: { _ in
779-
!SyncEngine.isSynchronizingChanges()
779+
!SyncEngine.$isSynchronizing
780780
}
781781
)
782782
```

Tests/SQLiteDataTests/CloudKitTests/SchemaChangeTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@
163163
│ ), │
164164
│ share: nil, │
165165
│ _isDeleted: false, │
166-
hasLastKnownServerRecord: true,
167-
isShared: false,
166+
_hasLastKnownServerRecord: true, │
167+
_isShared: false, │
168168
│ userModificationTime: 0 │
169169
│ ) │
170170
└────────────────────────────────────────────────────────────────────┘
@@ -251,8 +251,8 @@
251251
│ ), │
252252
│ share: nil, │
253253
│ _isDeleted: false, │
254-
hasLastKnownServerRecord: true,
255-
isShared: false,
254+
_hasLastKnownServerRecord: true, │
255+
_isShared: false, │
256256
│ userModificationTime: 1 │
257257
│ ) │
258258
└────────────────────────────────────────────────────────────────────┘
@@ -363,8 +363,8 @@
363363
│ ), │
364364
│ share: nil, │
365365
│ _isDeleted: false, │
366-
hasLastKnownServerRecord: true,
367-
isShared: false,
366+
_hasLastKnownServerRecord: true, │
367+
_isShared: false, │
368368
│ userModificationTime: 0 │
369369
│ ) │
370370
└────────────────────────────────────────────────────────────────────┘
@@ -410,8 +410,8 @@
410410
│ ), │
411411
│ share: nil, │
412412
│ _isDeleted: false, │
413-
hasLastKnownServerRecord: true,
414-
isShared: false,
413+
_hasLastKnownServerRecord: true, │
414+
_isShared: false, │
415415
│ userModificationTime: 0 │
416416
│ ) │
417417
└────────────────────────────────────────────────────────────────────┘

0 commit comments

Comments
 (0)