Michael Long
Jun 9, 2024

--

Prefer the approach I wrote about in the following article.

The onChange/switch is a complex, redundant way to accomplish what should be a simple task, and one that's easy to perform by simply attaching .task to ProgressView.

Further, I prefer using associated types with the enumerations, especially for .loaded(data). Doing as shown in the article has the potential to have state mismatch issues such that viewState is .loaded but randomInt is still nil, leaving the view in an inconsistent state. Again as mentioned in the following.

Finally, using DispatchQueue is awkward and heavy. Better to annotate with @MainActor.

https://medium.com/the-swift-cooperative/the-definitive-guide-to-viewstate-in-swiftui-9923afeb5455

--

--

Michael Long
Michael Long

Written by Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.

Responses (1)