sindresorhus / Defaults

💾 Swifty and modern UserDefaults
https://swiftpackageindex.com/sindresorhus/Defaults/documentation/defaults
MIT License
2.01k stars 120 forks source link

Make `@Default` usable in an `ObservableObject` #48

Open lisindima opened 4 years ago

lisindima commented 4 years ago

Good afternoon and thanks for this amazing package. I came across a small error, I can not get the view to be updated when I reset the value to default. When I close the application and open it again, I see that the value is written and is equal to the default.

class SessionStore: ObservableObject {
    @Default(.loginModel) var loginModel
}

extension Defaults.Keys {
    static let loginModel = Key<LoginModel?>("loginModel", default: nil)
}

Button(action:  {
    sessionStore.loginModel = nil 
}) {
     HStack {
          Image(systemName: "flame")
                   .frame(width: 24)
          Text("Выйти из аккаунта")
       }.foregroundColor(.red)
}
sindresorhus commented 4 years ago

You cannot use @Default in an ObservableObject. It's meant to be used in a View.

I agree, it would be useful to allow this, but it's currently not possible to replicate the functionality of @Published, since it uses internal runtime inspection. More info here: https://twitter.com/olebegemann/status/1222866304786423808


There are ways to make this partly work: https://stackoverflow.com/a/59039333/64949 But I'm inclined to wait for WWDC before doing anything more, as I'm hoping it will bring the improvements needed.

sindresorhus commented 4 years ago

Unrelated, but you don't need default: nil when the type is optional.

sindresorhus commented 3 years ago

From macOS 11.3 beta 2 release notes:

AppStorage property wrappers now work as expected when contained inside an ObservableObject , causing the system to emit the objectWillChange publisher. (65562845)

So we could maybe wrap @AppStorage or something.