pointfreeco / swift-composable-architecture

A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind.
https://www.pointfree.co/collections/composable-architecture
MIT License
12.3k stars 1.43k forks source link

Add Shared AppStorage support for [Bool], [Int], [Double] and [String] #3192

Closed johankool closed 3 months ago

johankool commented 3 months ago

My project needed to access an array of integers from user defaults.

mbrandonw commented 3 months ago

Hi @johankool, we have made the decision to not support things like this in .appStorage because neither SwiftUI's @AppStorage nor UserDefaults support it. Based on Apple's recommendation, one should only put small bits of simple data in user defaults, and so it seems they discourage putting arrays in a store. You still can do it, but you will need to encode it to data and then decode when reading. Or you could consider using .fileStorage instead.

I am going to close this PR for now, but do feel free to open a discussion if you have questions.

johankool commented 3 months ago

Hm, that's disappointing. I get your point about @AppStorage but UserDefaults quite specifically supports arrays and arrays of strings in particular. That the data should be simple/smallish is on the consumer of the API. Storing a few indices this way doesn't seem unreasonable to me.

Unfortunately it seems not enough is exposed of the AppStorageKey and the Lookups to be able to add this extension in my own code.

mbrandonw commented 3 months ago

You are right, user defaults does support arrays. However @AppStorage does not. I'm not sure why, perhaps just due to the overload space of trying to provide a type-safe interface to user defaults. Maybe that leads to compiler issues.

We could discuss making more of the internals public, but I'm still not sure why it isn't better to use .fileStorage?