'Difference between setting a variable using @EnvironmentObject and @MainActor
Let's say that I have the following:
@MainActor
final class NetworkController: ObservableObject {
@Published var authState: AuthState = .guess
}
I understand that if I have a function inside that class that sets the authState to other state, that would trigger the UI response, but is that the same as me setting it in a view using @EnvironmentObject?
Example 1: Setting it inside the class
@MainActor
final class NetworkController: ObservableObject {
@Published var authState: AuthState = .guess
func setState() {
authState = .login
}
}
then either in another viewmodel or onAppear in a view calling that function.
Example 2: Setting it in the environment
struct SessionControllerView: View {
@EnvironmentObject networkController: NetworkController
var body: some View {
Text("Something")
.onAppear {
networkController.authState = .login
}
}
}
Thanks
EDIT:
This is an overlooked question, Apple is making some changes between these two, they are not the same.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
