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

@StoredValue set(_ value: Item) not working as expected. #27

Closed JaidynBluesky closed 1 year ago

JaidynBluesky commented 1 year ago

Preface: we were chatting on Twitter yesterday about the .documentsDirectory issue. I've switched to branch 2.0 and am trying some stuff out, so I understand the documentation isn't complete yet. :)

I have initialised a @StoredValue in one of my services like so:

@StoredValue<UserModel>(key: "User")
private var storedUser

where UserModel is Codable, Equatable and Identifiable.

From the existing file documentation, it seems like I just need to call

storedUser.set(newUser)

but that is giving me the following errors:

Screen Shot 2022-08-02 at 10 48 34 am

I've also tried awaiting the .set() function, but that doesn't solve it either.

Many thanks!

mergesort commented 1 year ago

Hey @JaidynBluesky! Your code is pretty close, instead of storedUser.set(newUser) you should be calling $storedUser.set(newUser).

If you're familiar with property wrappers, the wrappedValue is an Item, but the projectedValue is a StoredValue<Item>. That means that when you access storedUser the item you're interacting with is of type Item, but the projected value is the one that contains all of the functions, including set() and reset().

This follows similar conventions to @Published var items: [Item] would let you use items as a regular [Item], but $items projects AnyPublisher<[Item], Never> so you can subscribe to changes items produces. Within Boutique the @Stored property wrapper works very similarly.

Hope that clears things and lemme know if it works! I'll make sure to add some documentation inline above the set() and reset() functions, and have taken a note down to explain this in the walkthrough for @StoredValue.

JaidynBluesky commented 1 year ago

Ah, that would be why it's not working! 😄

edit: this works.