Modeling substate as an enum with associated values #1622
-
I have a Todo list screen that is split into 3 reducers:
My TodoList.State has a /// Represents the different states of a piece of loadable substate.
public enum LoadingState<LoadedState, LoadError>: Equatable where LoadedState: Equatable, LoadError: Equatable, LoadError: Error {
/// Substate has not yet been loaded.
case notLoaded
/// Substate is in the process of being loaded.
case loading
/// Substate has successfully loaded.
case loaded(LoadedState)
/// Substate has failed to load.
case failed(LoadError)
} In this instance, the I'm in the process of migrating these reducers to the ReducerProtocol and have encountered a large degradation in performance. After using some of the debugging tools available on I was able to work around this issue by manually adding the equality conformance and ignoring the associated values like so: public enum LoadingState... {
... // *snip
public var discriminator: Discriminator {
switch self {
case .notLoaded: return .notLoaded
case .loading: return .loading
case .loaded: return .loaded
case .failed: return .failed
}
}
public enum Discriminator: String, CaseIterable {
case notLoaded, loading, loaded, failed
}
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.discriminator == rhs.discriminator
}
} So here's my question: Is this a reasonable workaround for the issue I was encountering or is there a different tool I should be reaching for here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
@rzulkoski I think we need a little more context and code to debug the issue, but glad you found a workaround!
I'm not quite sure I understand how migrating to the TCA provides Are you using |
Beta Was this translation helpful? Give feedback.
@rzulkoski I think we need a little more context and code to debug the issue, but glad you found a workaround!
I'm not quite sure I understand how migrating to the
ReducerProtocol
would introduce performance degradation at the view layer. Did you make changes to the view as well?TCA provides
SwitchStore
precisely to work around the problem of enum state performance in SwiftUI by limiting updates based on the enum tag:swift-composable-architecture/Sources/ComposableArchitecture/SwiftUI/SwitchStore.swift
Line 176 in 3bfbc7f