ValueStatus

public enum ValueStatus<Value, ErrorType>: Equatable
where Value: Equatable, ErrorType: Error & Equatable

ValueStatus is a helpful enum which can be used to describe the state of a long running action that involves a value such as; fetching data from an API.

An example of what this could look like being used.

struct AppState: Equatable {
    var blogPosts: ValueStatus<[BlogPost], APIError> = .idle
}
  • Status is in an idle state and hasn’t yet started.

    Declaration

    Swift

    case idle
  • Status is in a loading state.

    The loading status can provide access to previously loaded value. This can be used to display the previously loaded value along with a loading indicator.

    Declaration

    Swift

    case loading(Value?)
  • Status is complete.

    Declaration

    Swift

    case complete(Value)
  • Status has failed.

    The failed status can provide access to previously loaded value. This can be used to display the previously loaded value along with a error message.

    Declaration

    Swift

    case failed(ErrorType, Value?)

Helpers

  • Indicates whether ValueStatus is in an idle state.

    Declaration

    Swift

    public var isIdle: Bool { get }
  • Indicates whether ValueStatus is in a loading state.

    Declaration

    Swift

    public var isLoading: Bool { get }
  • Indicates whether ValueStatus is in a complete state.

    Declaration

    Swift

    public var isComplete: Bool { get }
  • Indicates whether ValueStatus is in a failed state.

    Declaration

    Swift

    public var isFailed: Bool { get }
  • Convenience accessor to the error if the status is in a failed state.

    Declaration

    Swift

    public var error: ErrorType? { get }
  • Convenience accessor to the value if the status is in a complete state.

    Declaration

    Swift

    public var value: Value? { get }
  • Convenience accessor to the latest value of the status.

    This slightly differs from value because the value could be coming from loading or failed, depending on the current status.

    Example:

    • .idle
      • latestValue => nil
      • value => nil
    • .loading(nil)
      • latestValue => nil
      • value => nil
    • .complete("value")
      • latestValue => “value”
      • value => “value”
    • .loading("value")
      • latestValue => “value”
      • value => nil

    Declaration

    Swift

    public var latestValue: Value? { get }