Open iband opened 1 year ago
It is correct that @CloudStorage
currently doesn't work inside @Observable
, same as @AppStorage
.
Same as with AppStorage, you can workaround this by manually implementing a getter and setter for a property:
@Observable @MainActor class ViewModel {
var progress: Double? {
get {
access(keyPath: \.progress)
return CloudStorageSync.shared.double(for: "progress")
}
set {
withMutation(keyPath: \.progress) {
CloudStorageSync.shared.set(newValue, for: "progress")
}
}
}
}
Basically the same issue as with not being able to use @AppStorage inside @Observable (workaround for @AppStorage was suggested here https://medium.com/@davidsteppenbeck/accessing-userdefaults-within-observable-classes-in-ios-17-8cb46ca75689)