Thanks, @mpsnp. I 100% agree. However, even if the act of setting persisted values is moved to the environment, using Effect
, there's still an open question of how to load persisted values at the time a container State
is initialized.
Consider the following example:
struct SomeState: Equatable {
// How do we load the initial value upon initialization?
var persistedValue: Int
}
enum SomeAction: Equatable {
case setPersistedValue(Int)
}
struct SomeEnvironment {
var persistenceClient: Client
}
let someReducer = Reducer<SomeState, SomeAction, SomeEnvironment> { state, action, environment in
switch action {
case .setPersistedValue(let value):
return environment.persistenceClient
.set(value, for: "someKey")
.eraseToEffect()
}
}
One option I can think of is sending an action to the store on the onAppear
event, which would also use the environment to fetch and set persistedValue
's initial value. However, that might be too late, and would also force the value type to be optional, to allow for this kind of "lazy-loading".
Any ideas? Thanks.