Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@CloudStorage doesn't workin inside @Observable #10

Open
iband opened this issue Sep 26, 2023 · 1 comment
Open

@CloudStorage doesn't workin inside @Observable #10

iband opened this issue Sep 26, 2023 · 1 comment

Comments

@iband
Copy link

iband commented Sep 26, 2023

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)

@tomlokhorst
Copy link
Member

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")
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants