@@ -72,7 +72,7 @@ final class iCloudSynchronizer {
72
72
73
73
self . enqueue {
74
74
self . recordTimestamp ( forKey: key, timestamp: Self . timestamp, source: . local)
75
- await self . syncKey ( key: key , . local)
75
+ await self . syncKey ( key, source : . local)
76
76
}
77
77
}
78
78
@@ -125,7 +125,7 @@ final class iCloudSynchronizer {
125
125
for key in keys {
126
126
let latest = source ?? latestDataSource ( forKey: key)
127
127
self . enqueue {
128
- await self . syncKey ( key: key , latest)
128
+ await self . syncKey ( key, source : latest)
129
129
}
130
130
}
131
131
}
@@ -154,7 +154,7 @@ final class iCloudSynchronizer {
154
154
- Parameter key: The key to synchronize.
155
155
- Parameter source: Sync key from which data source (remote or local).
156
156
*/
157
- private func syncKey( key: Defaults . Keys , _ source: Defaults . DataSource ) async {
157
+ private func syncKey( _ key: Defaults . Keys , source: Defaults . DataSource ) async {
158
158
Self . logKeySyncStatus ( key, source: source, syncStatus: . idle)
159
159
160
160
switch source {
@@ -227,7 +227,7 @@ final class iCloudSynchronizer {
227
227
228
228
The timestamp storage format varies across different source providers due to storage limitations.
229
229
*/
230
- private func timestamp( forKey key: Defaults . Keys , _ source: Defaults . DataSource ) -> Date ? {
230
+ private func timestamp( forKey key: Defaults . Keys , source: Defaults . DataSource ) -> Date ? {
231
231
switch source {
232
232
case . remote:
233
233
guard
@@ -277,10 +277,10 @@ final class iCloudSynchronizer {
277
277
*/
278
278
private func latestDataSource( forKey key: Defaults . Keys ) -> Defaults . DataSource {
279
279
// 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 {
281
281
return . local
282
282
}
283
- guard let localTimestamp = self . timestamp ( forKey: key, . local) else {
283
+ guard let localTimestamp = self . timestamp ( forKey: key, source : . local) else {
284
284
return . remote
285
285
}
286
286
@@ -306,14 +306,14 @@ extension iCloudSynchronizer {
306
306
. store ( in: & cancellables)
307
307
308
308
// 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)
310
310
NotificationCenter . default
311
311
. publisher ( for: UIScene . willEnterForegroundNotification)
312
312
#elseif os(watchOS)
313
313
NotificationCenter . default
314
314
. publisher ( for: WKExtension . applicationWillEnterForegroundNotification)
315
315
#endif
316
- #if os(iOS) || os(tvOS) || os(watchOS)
316
+ #if os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)
317
317
. sink { [ weak self] notification in
318
318
guard let self else {
319
319
return
@@ -344,18 +344,18 @@ extension iCloudSynchronizer {
344
344
}
345
345
346
346
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 {
348
348
continue
349
349
}
350
350
if
351
- let localTimestamp = self . timestamp ( forKey: key, . local) ,
351
+ let localTimestamp = self . timestamp ( forKey: key, source : . local) ,
352
352
localTimestamp >= remoteTimestamp
353
353
{
354
354
continue
355
355
}
356
356
357
357
self . enqueue {
358
- await self . syncKey ( key: key , . remote)
358
+ await self . syncKey ( key, source : . remote)
359
359
}
360
360
}
361
361
}
@@ -365,7 +365,7 @@ extension iCloudSynchronizer {
365
365
`iCloudSynchronizer` logging related functions.
366
366
*/
367
367
extension iCloudSynchronizer {
368
- @available ( macOS 11 , iOS 14 , tvOS 14 , watchOS 7 , * )
368
+ @available ( macOS 11 , iOS 14 , tvOS 14 , watchOS 7 , visionOS 1 , * )
369
369
private static let logger = Logger ( OSLog . default)
370
370
371
371
private static func logKeySyncStatus( _ key: Defaults . Keys , source: Defaults . DataSource , syncStatus: SyncStatus , value: Any ? = nil ) {
@@ -439,12 +439,13 @@ extension Defaults {
439
439
/**
440
440
Synchronize values with different devices over iCloud.
441
441
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` `:
443
443
444
444
1. Using ``iCloud/add(_:)-5gffb``
445
445
2. Utilizing ``iCloud/syncWithoutWaiting(_:source:)-9cpju``
446
446
3. Observing UserDefaults for added ``Defaults/Defaults/Key`` using Key-Value Observation (KVO)
447
447
4. Monitoring `NSUbiquitousKeyValueStore.didChangeExternallyNotification` for added ``Defaults/Defaults/Key``.
448
+ 5. Initializing ``Defaults/Defaults/Keys`` with parameter `iCloud: true`.
448
449
449
450
> Tip: After initializing the task, we can call ``iCloud/sync()`` to ensure that all tasks in the backgroundQueue are completed.
450
451
@@ -458,7 +459,7 @@ extension Defaults {
458
459
Task {
459
460
let quality = Defaults.Key<Int>("quality", default: 0)
460
461
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 .
462
463
// Both `isUnicornMode` and `quality` are synced.
463
464
}
464
465
```
@@ -470,7 +471,7 @@ extension Defaults {
470
471
static var synchronizer = iCloudSynchronizer ( remoteStorage: NSUbiquitousKeyValueStore . default)
471
472
472
473
/**
473
- Lists the synced keys.
474
+ The synced keys.
474
475
*/
475
476
public static var keys : Set < Defaults . Keys > { synchronizer. keys }
476
477
@@ -481,6 +482,7 @@ extension Defaults {
481
482
482
483
/**
483
484
Enable this if you want to debug the syncing status of keys.
485
+ Logs will be printed to the console in OSLog format.
484
486
485
487
- Note: The log information will include details such as the key being synced, its corresponding value, and the status of the synchronization.
486
488
*/
0 commit comments