The Definitive Guide to ViewState in SwiftUI
Even if you’re doing it, you’re probably doing it wrong….
SwiftUI Views are not views in the traditional sense. They’re lightweight descriptions of our desired user interface, generated by merging our application data with a set of structures that define our layout and desired behavior.
Core to getting the correct definition is the concept of a single source of truth, the idea that each element is driven by one — and only one — piece of data. And when that data changes, our interface changes to match.
It’s a powerful concept. Adhere to it and many of the problems inherent in imperative programming simply disappear.
But sometimes it doesn’t seem to be that easy. What happens when our interface is driven by multiple pieces of information? How do we make sure we’re generating the correct definition and displaying the correct information?
Well, a somewhat flippant answer to that question would be… just don’t do that.
But as it turns out, that’s also the right answer.
I’m going to demonstrate this with a fairly common example.
The Loading View
We’ve all created loading views and we all understand the inherent problems, but…