Skip to content

Commit ccea22a

Browse files
committed
feat: apply comments
1 parent 6d68726 commit ccea22a

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

Sources/Defaults/Defaults+iCloud.swift

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ final class iCloudSynchronizer {
7272

7373
self.enqueue {
7474
self.recordTimestamp(forKey: key, timestamp: Self.timestamp, source: .local)
75-
await self.syncKey(key: key, .local)
75+
await self.syncKey(key, source: .local)
7676
}
7777
}
7878

@@ -125,7 +125,7 @@ final class iCloudSynchronizer {
125125
for key in keys {
126126
let latest = source ?? latestDataSource(forKey: key)
127127
self.enqueue {
128-
await self.syncKey(key: key, latest)
128+
await self.syncKey(key, source: latest)
129129
}
130130
}
131131
}
@@ -154,7 +154,7 @@ final class iCloudSynchronizer {
154154
- Parameter key: The key to synchronize.
155155
- Parameter source: Sync key from which data source (remote or local).
156156
*/
157-
private func syncKey(key: Defaults.Keys, _ source: Defaults.DataSource) async {
157+
private func syncKey(_ key: Defaults.Keys, source: Defaults.DataSource) async {
158158
Self.logKeySyncStatus(key, source: source, syncStatus: .idle)
159159

160160
switch source {
@@ -227,7 +227,7 @@ final class iCloudSynchronizer {
227227

228228
The timestamp storage format varies across different source providers due to storage limitations.
229229
*/
230-
private func timestamp(forKey key: Defaults.Keys, _ source: Defaults.DataSource) -> Date? {
230+
private func timestamp(forKey key: Defaults.Keys, source: Defaults.DataSource) -> Date? {
231231
switch source {
232232
case .remote:
233233
guard
@@ -277,10 +277,10 @@ final class iCloudSynchronizer {
277277
*/
278278
private func latestDataSource(forKey key: Defaults.Keys) -> Defaults.DataSource {
279279
// If the remote timestamp does not exist, use the local timestamp as the latest data source.
280-
guard let remoteTimestamp = self.timestamp(forKey: key, .remote) else {
280+
guard let remoteTimestamp = self.timestamp(forKey: key, source: .remote) else {
281281
return .local
282282
}
283-
guard let localTimestamp = self.timestamp(forKey: key, .local) else {
283+
guard let localTimestamp = self.timestamp(forKey: key, source: .local) else {
284284
return .remote
285285
}
286286

@@ -306,14 +306,14 @@ extension iCloudSynchronizer {
306306
.store(in: &cancellables)
307307

308308
// TODO: Replace it with async stream when Swift supports custom executors.
309-
#if os(iOS) || os(tvOS)
309+
#if os(iOS) || os(tvOS) || os(visionOS)
310310
NotificationCenter.default
311311
.publisher(for: UIScene.willEnterForegroundNotification)
312312
#elseif os(watchOS)
313313
NotificationCenter.default
314314
.publisher(for: WKExtension.applicationWillEnterForegroundNotification)
315315
#endif
316-
#if os(iOS) || os(tvOS) || os(watchOS)
316+
#if os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)
317317
.sink { [weak self] notification in
318318
guard let self else {
319319
return
@@ -344,18 +344,18 @@ extension iCloudSynchronizer {
344344
}
345345

346346
for key in self.keys where changedKeys.contains(key.name) {
347-
guard let remoteTimestamp = self.timestamp(forKey: key, .remote) else {
347+
guard let remoteTimestamp = self.timestamp(forKey: key, source: .remote) else {
348348
continue
349349
}
350350
if
351-
let localTimestamp = self.timestamp(forKey: key, .local),
351+
let localTimestamp = self.timestamp(forKey: key, source: .local),
352352
localTimestamp >= remoteTimestamp
353353
{
354354
continue
355355
}
356356

357357
self.enqueue {
358-
await self.syncKey(key: key, .remote)
358+
await self.syncKey(key, source: .remote)
359359
}
360360
}
361361
}
@@ -365,7 +365,7 @@ extension iCloudSynchronizer {
365365
`iCloudSynchronizer` logging related functions.
366366
*/
367367
extension iCloudSynchronizer {
368-
@available(macOS 11, iOS 14, tvOS 14, watchOS 7, *)
368+
@available(macOS 11, iOS 14, tvOS 14, watchOS 7, visionOS 1, *)
369369
private static let logger = Logger(OSLog.default)
370370

371371
private static func logKeySyncStatus(_ key: Defaults.Keys, source: Defaults.DataSource, syncStatus: SyncStatus, value: Any? = nil) {
@@ -439,12 +439,13 @@ extension Defaults {
439439
/**
440440
Synchronize values with different devices over iCloud.
441441

442-
There are four ways to initiate synchronization, each of which will create a task in `backgroundQueue`:
442+
There are five ways to initiate synchronization, each of which will create a synchronization task in ``Defaults/iCloud/iCloud``:
443443

444444
1. Using ``iCloud/add(_:)-5gffb``
445445
2. Utilizing ``iCloud/syncWithoutWaiting(_:source:)-9cpju``
446446
3. Observing UserDefaults for added ``Defaults/Defaults/Key`` using Key-Value Observation (KVO)
447447
4. Monitoring `NSUbiquitousKeyValueStore.didChangeExternallyNotification` for added ``Defaults/Defaults/Key``.
448+
5. Initializing ``Defaults/Defaults/Keys`` with parameter `iCloud: true`.
448449

449450
> Tip: After initializing the task, we can call ``iCloud/sync()`` to ensure that all tasks in the backgroundQueue are completed.
450451

@@ -458,7 +459,7 @@ extension Defaults {
458459
Task {
459460
let quality = Defaults.Key<Int>("quality", default: 0)
460461
Defaults.iCloud.add(quality)
461-
await Defaults.iCloud.sync() // Using sync to make sure all synchronization tasks are done.
462+
await Defaults.iCloud.sync() // Optional step: only needed if you require everything to be synced before continuing.
462463
// Both `isUnicornMode` and `quality` are synced.
463464
}
464465
```
@@ -470,7 +471,7 @@ extension Defaults {
470471
static var synchronizer = iCloudSynchronizer(remoteStorage: NSUbiquitousKeyValueStore.default)
471472

472473
/**
473-
Lists the synced keys.
474+
The synced keys.
474475
*/
475476
public static var keys: Set<Defaults.Keys> { synchronizer.keys }
476477

@@ -481,6 +482,7 @@ extension Defaults {
481482

482483
/**
483484
Enable this if you want to debug the syncing status of keys.
485+
Logs will be printed to the console in OSLog format.
484486

485487
- Note: The log information will include details such as the key being synced, its corresponding value, and the status of the synchronization.
486488
*/

Sources/Defaults/Defaults.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ extension Defaults {
103103
Create a key.
104104

105105
- Parameter name: The name must be ASCII, not start with `@`, and cannot contain a dot (`.`).
106-
- Parameter iCloud: Set `true` if you want automatic synchronization to iCloud.
106+
- Parameter iCloud: Automatically synchronize the value with ``Defaults/Defaults/iCloud``.
107107

108108
The `default` parameter should not be used if the `Value` type is an optional.
109109
*/
@@ -148,6 +148,7 @@ extension Defaults {
148148
```
149149

150150
- Parameter name: The name must be ASCII, not start with `@`, and cannot contain a dot (`.`).
151+
- Parameter iCloud: Automatically synchronize the value with ``Defaults/Defaults/iCloud``.
151152

152153
- Note: This initializer will not set the default value in the actual `UserDefaults`. This should not matter much though. It's only really useful if you use legacy KVO bindings.
153154
*/

Sources/Defaults/Utilities.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ final class TaskQueue {
365365
}
366366
}
367367

368+
// TODO: replace with Swift 6 native Atomics support.
368369
@propertyWrapper
369370
final class Atomic<Value> {
370371
private let lock: Lock = .make()

0 commit comments

Comments
 (0)