Skip to content

Commit

Permalink
Merge pull request #15 from Tabonx/main
Browse files Browse the repository at this point in the history
Dispatch KVS notifications to MainActor context
  • Loading branch information
tomlokhorst authored Nov 8, 2024
2 parents 766a589 + bec656c commit 62db0c9
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Sources/CloudStorage/CloudStorageSync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ public final class CloudStorageSync: ObservableObject {
status = Status(date: Date(), source: .initial, keys: [])

NotificationCenter.default.addObserver(
self,
selector: #selector(didChangeExternally(notification:)),
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
object: nil)
forName: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
object: nil,
queue: .main
) { [weak self] notification in
guard let self else { return }
MainActor.assumeIsolated {
self.didChangeExternally(notification: notification)
}
}
ubiquitousKvs.synchronize()

#if canImport(UIKit) && !os(watchOS)
Expand All @@ -41,7 +46,7 @@ public final class CloudStorageSync: ObservableObject {
#endif
}

@objc private func didChangeExternally(notification: Notification) {
private func didChangeExternally(notification: Notification) {
let reasonRaw = notification.userInfo?[NSUbiquitousKeyValueStoreChangeReasonKey] as? Int ?? -1
let keys = notification.userInfo?[NSUbiquitousKeyValueStoreChangedKeysKey] as? [String] ?? []
let reason = ChangeReason(rawValue: reasonRaw)
Expand Down

0 comments on commit 62db0c9

Please sign in to comment.