Run timer using composable architecture iOS

127 Views Asked by At

I am learning TCA and having an issue with running timer befor I did it like this

EffectTask.timer(id: TimerID.refresh, every: 5, on: DispatchQueue.main)

but now it is depreciate can someone help thanks in advance

1

There are 1 best solutions below

0
On

You need to use the clock dependency, your code will look something like this:

@Reducer
struct FeatureReducer {

  struct State: Equatable {
    var timer = 0
  }

  enum Action {
    case startTimer
    case updateTimer
  }

  @Dependency(\.continuousClock) var clock

  var body: some Reducer<State, Action> {
    switch action {
    case .startTimer:
      return .run { send in
        for await _ in self.clock.timer(interval: .seconds(1)) {
          await send(.timerUpdated)
        }
      }
    case .updateTimer:
      state.timer += 1
      return .none
  }
}