How to sequence multiple process steps with timers?

Need a little help/guidance with the architecture / structure of the app I’m working on.

I have an array of “step” structs, and I need to loop through each step in the array and sequentially pass each struct to a StepView. StepView starts a timer, and when the timer expires, automatically closes.

struct ProtocolStep: Identifiable, Decodable {
let id:UUID=UUID()
var step: Int
var type: String
var time: Int // time in seconds

I have all of this working for ONE step using a simple NavigationLink. Here is my call:

NavigationLink(destination: StepView(sessionType: “protocol”, step:hotColdProtocol.protocolSteps[0]))

What I need help with is when StepView closes, I need the calling function/view to then pass the next “step” back into that StepView. Repeat until I’ve gone through all of the steps.

I still need the initial click on the NavigationLink to kick off the process, but once that happens I want the steps to be looped through automatically.

I guess I could also pass the entire array into my StepView and try to control everything inside that view…when the timer expires, get the next Step struct from the array passed in, set the relevant variables, and start the next timer…? (I just thought of this as I was typing out this post, actually.)

Appreciate any thoughts/advice/code/pseudocode you might have for doing something like this…


Hi Brent

Welcome to the community.

The thing is that if you use the NavigationLink to kick off the child View process, when you come back to this parent view it won’t have any automatic way to pick the next timer in the array to then call that View again.

What you are better off doing is using an .onTapGesture which calls a function to control the process of looping through the array which triggers the child process each time. There are a few complications in that as well since the child view will need to be presented as a .sheet (or a .fullScreenCover) and therefore needs to be triggered by a boolean. When the child view finishes you can use an .onDismiss closure attached to the .sheet modifier to control what happens next if there are still items in the array to be actioned. The function that controls the looping might need to have an escape boolean which is set in the .onDismiss closure.

This is all doable but some thought needs to be put into how to execute it.

Does this make sense?

Thanks Chris, I get most of what you’re saying here, and can tell you see the same complications I do. Appreciate the bit of guidance, will give it a go and post more if I have specific questions or problems with getting it to work.