There is shift in programming paradigm from Imperative to Declarative.
Autoupdating the view whenever there are changes in the model.
SwiftUI provides way to share data across all views within app, using @EnviornmentObject. This enables the app to update the views automatically whenever data changes.
Scenerios:
If we want to pass data from A -> B -> C, instead of creating protocols or singleton objects, we can create @EnvironmentObject and put into the environment object so that views B, C and D can automatically have access to it.
Precaution:
Environment objects must be supplied by an ancestor views, if wont find the object, it may lead to crash.
Usage:
Object or Model that needs to be shared across views must confirms to protocol ObservableObject.
Change in the model also communicated with the help of @Published property wrappers.
By default an ObservableObject synthesizes an objectWillChange publisher that emits the changed value before any of its @Published properties changes.