mergesort / Boutique

✨ A magical persistence library (and so much more) for state-driven iOS and Mac apps ✨
https://build.ms/boutique/docs
MIT License
899 stars 43 forks source link

Make @Stored usable in UIKit/AppKit apps #21

Closed mergesort closed 1 year ago

mergesort commented 2 years ago

Right now the @Stored property wrapper has an implicit assumption that the parent is an ObservableObject so the Store's changes can be republished. We can't make that assumption in an AppKit or UIKit app, so we either need a new construct to handle that, or to loosen the restriction.

The approach I'm leaning towards is conditionally casting the ObservableObject check in @Stored's subscript rather than a generic constraint of [Object] where Instance.ObjectWillChangePublisher == ObservableObjectPublisher which enforces these semantics.

This is not a top priority but I would like to address it so people can use it to meet the needs of their apps.

subdigital commented 2 years ago

I was playing around with this, thinking that maybe you could have 2 static subscripts as long as they had different types. In theory this should work just fine, but property wrappers currently don't allow this

Property wrapper type 'Stored' has multiple enclosing-self subscripts 'subscript(_enclosingInstance:wrapped:storage:)'

My 2nd thought was to provide a protocol:

protocol NotifyChanges {
    var objectWillChange: PassthroughSubject<Void, Never> { get }
}

The name is dumb, but you get the idea. Unfortunately we can't extend the ObservableObject protocol to add this conformance, so this would require people adopt this protocol in their own types.

Ultimately I think the easiest solution would be what you said and do some conditional type checks for ObservableObject and some custom protocol.

I know this isn't super urgent, but it was fun to tinker around with and perhaps these findings save you some time.

mergesort commented 1 year ago

Forgot to close this out a while ago but Boutique has been usable in UIKit/AppKit apps for a few months now, without any modifications needed. Enjoy! 😄