View will lose data of ObservedObject in a fullScreenCover when EditMode changes

I've created a view with a TextField and an EditButton. When I open this view in a fullScreenCover with the editMode active than the input text disappears when the Done-Button is tapped.

This behavior is appearing when a @ObservedObject is used. When a @State variable is used for the TextField then everything is fine.

This is the view
Code Block Swift
struct ProfileView: View {
@ObservedObject
var viewModel = ProfileViewModel()
@Environment(\.editMode) var editMode
@State
var name = ""
var body: some View {
VStack {
Text("@State")
TextField("TextField", text: $name)
.disabled(editMode?.wrappedValue == .inactive)
Text("@ObservedObject")
TextField("TextField", text: $viewModel.name)
.disabled(editMode?.wrappedValue == .inactive)
Spacer()
}
.padding()
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
}
}
}

Using

Code Block Swift
class ProfileViewModel: ObservableObject {
@Published var name: String = ""
}


This is the view which is presenting
Code Block Swift
struct ContentView: View {
@State
var isPresentingEmtpyProfile: Bool = false
var body: some View {
NavigationView {
List {
NavigationLink(destination: ProfileView()) {
Text("ProfileView")
}
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
isPresentingEmtpyProfile = true
}) {
Image(systemName: "plus")
}
}
}
.navigationBarTitle("Navigation", displayMode: .inline)
.fullScreenCover(isPresented: $isPresentingEmtpyProfile) {
NewProfileViewWithNavigation(isPresentingEmptyProfile: $isPresentingEmtpyProfile)
}
}
}
}
extension ContentView {
struct NewProfileViewWithNavigation: View {
@State
private var editMode = EditMode.active
@Binding
var isPresentingEmptyProfile: Bool
var body: some View {
NavigationView {
ProfileView()
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(action: {
isPresentingEmptyProfile = false
}) {
Text("Close")
}
}
ToolbarItem(placement: .principal) {
Text("New Profile")
}
}
.environment(\.editMode, $editMode)
}
}
}
}


Does anyone have an idea why this is happening?

Answered by OOPer in 673131022
@ObservedObject properties may be re-initialized at each time the containing view is initialized. And it is hard to predict when views are initialized in SwiftUI.

Have you tried @StateObject?
Code Block
@StateObject
var viewModel = ProfileViewModel()


Accepted Answer
@ObservedObject properties may be re-initialized at each time the containing view is initialized. And it is hard to predict when views are initialized in SwiftUI.

Have you tried @StateObject?
Code Block
@StateObject
var viewModel = ProfileViewModel()


Thank so much. This helped. In my case it makes sense to use @StateObject because the view creates and owns the view model. @ObservedObject is used when someone else is holding the observed object.
View will lose data of ObservedObject in a fullScreenCover when EditMode changes
 
 
Q