-
Hello, I need some help figuring out a crash. Therefore I have 3 reducers of concern (AppFeature, FeedFeature and SettingsFeature). They look like this (with unnecessary parts removed for better readability): AppFeature Reducer@Reducer
public struct AppFeature {
public enum Tab: Hashable, Equatable, Sendable, Identifiable, CaseIterable {
case feed
public var id: Self { self }
}
@ObservableState
public struct State {
public var selectedTab: Tab?
public var feed: FeedFeature.State
public init(
selectedTab: Tab = .feed,
feed: FeedFeature.State = .init()
) {
self.selectedTab = selectedTab
self.feed = feed
}
}
public enum Action: ViewAction {
@CasePathable
public enum View {
case tabSelected(Tab?)
}
case view(View)
case feed(FeedFeature.Action)
}
public init() {}
public var body: some ReducerOf<Self> {
Scope(state: \.feed, action: /Action.feed) {
FeedFeature()
}
Reduce { state, action in
switch action {
case let .view(.tabSelected(tab)):
state.selectedTab = tab
return .none
case .feed:
return .none
}
}
}
} FeedFeature Reducer@Reducer
public struct FeedFeature {
@ObservableState
public struct State {
@Presents public var settings: SettingsFeature.State?
public init(settings: SettingsFeature.State? = nil) {
self.settings = settings
}
}
public enum Action: ViewAction {
@CasePathable
public enum View {
case showSettings
}
case view(View)
case settings(PresentationAction<SettingsFeature.Action>)
}
public init() {}
public var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .view(.showSettings):
state.settings = .init()
return .none
case .settings:
return .none
}
}
}
} SettingsFeature Reducer@Reducer
public struct SettingsFeature {
@ObservableState
public struct State {
public var serverPicker: ServerPickerFeature.State
public var licenses: LicenseFeature.State
public var path: StackState<Path.State>
public init(
serverPicker: ServerPickerFeature.State = .init(),
licenses: LicenseFeature.State = .init(),
path: StackState<Path.State> = .init()
) {
self.serverPicker = serverPicker
self.licenses = licenses
self.path = path
}
}
public enum Action {
case serverPicker(ServerPickerFeature.Action)
case licenses(LicenseFeature.Action)
case path(StackAction<Path.State, Path.Action>)
}
public init() {}
public var body: some ReducerOf<Self> {
Scope(state: \.serverPicker, action: /Action.serverPicker) {
ServerPickerFeature()
}
Scope(state: \.licenses, action: /Action.licenses) {
LicenseFeature()
}
Reduce { state, action in
switch action {
case .serverPicker:
return .none
case .licenses:
return .none
case .path:
return .none
}
}
.forEach(\.path, action: \.path) {
Path()
}
}
@Reducer
public struct Path {
@ObservableState
public enum State {
case serverPicker(ServerPickerFeature.State)
case licenses(LicenseFeature.State)
}
public init() {}
public enum Action {
case serverPicker(ServerPickerFeature.Action)
case licenses(LicenseFeature.Action)
}
public var body: some ReducerOf<Self> {
Scope(state: /State.serverPicker, action: /Action.serverPicker) {
ServerPickerFeature()
}
Scope(state: /State.licenses, action: /Action.licenses) {
LicenseFeature()
}
}
}
} From the FeedView (via a toolbar button) I send the showSettings view action to open the settings sheet which is shown via: .sheet(item: $store.scope(state: \.settings, action: \.settings)) { store in
SettingsView(store: store)
} Opening the settings works but when navigating inside of settings and going back I get a crash: I see the following log when printing changes on my AppFeature Reducer:
So I guess I am missing a Scope somewhere in my reducers? Probably SettingsFeature is missing in the FeedFeature Reducer? However I am not sure how to add this to my reducer as I am having an optional state and can't seem to make it compile. Or am I on the wrong track and something different is wrong in my implementation? Kind regards, I would love some help on this. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Nevermind and sorry to bother you. I figured it out once I saw this syntax in an issue while browsing for a solution: .ifLet(\.$settings, action: \.settings) {
SettingsFeature()
} to my FeedFeature Reducer resolved the issue. |
Beta Was this translation helpful? Give feedback.
Nevermind and sorry to bother you. I figured it out once I saw this syntax in an issue while browsing for a solution:
Adding
to my FeedFeature Reducer resolved the issue.