Skip to content

Looking for the right timer pattern (update UI based on elapsed time) #1909

Answered by tgrapperon
sbeitzel asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @sbeitzel! There are two approaches to your problem. The first one consists in performing this operation in TCA, the other one, in SwiftUI (for example if the elapsed time value doesn't participate to your business logic).

For the first approach, you will need a timer. The simplest is probably to use @Dependency(\.continuousClock) that has a timer utility. This thing is an async sequence, so you can iterate in a .run effect that you start when the view appears (in .task for example):

@Dependency(\.continuousClock) var clock
var body: some Reducer {
  Reduce { state, action in
    switch action {
    case .task:
      return .run { send in
        for await _ in self.clock.timer(interval:

Replies: 2 comments 9 replies

Comment options

You must be logged in to vote
5 replies
@tgrapperon
Comment options

@rpatel8del
Comment options

@tgrapperon
Comment options

@rpatel8del
Comment options

@kakhi-visionprism
Comment options

Answer selected by sbeitzel
Comment options

You must be logged in to vote
4 replies
@xuanyuelin
Comment options

@mbrandonw
Comment options

@BeetLab
Comment options

@mbrandonw
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
8 participants