UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Dismissing view using ObservableObject class provokes Publishing changes from within view updates is not allowed

Forums > SwiftUI

Hi!

I do not understand this behavior: When dismissing a fullScreenCover using a variable inside an ObservableObject (lines commented with 1.-) it shows the "Publishing changes from within view updates is not allowed, this will cause undefined behavior." message in the console, but using a @State variable (lines commented with 2.-) does not show the warning. Here is the example code:

import SwiftUI

final class DismissWarningVM: ObservableObject {
    @Published var showAnotherView = false
}

struct DismissWarningView: View {
    @StateObject private var dismissWarningVM = DismissWarningVM()

    @State private var showAnotherView = false

    var body: some View {
        VStack {
            HStack {
                Spacer()
                Button {
                    // 1.- This line provokes the warning
                    dismissWarningVM.showAnotherView = true
                    // 2.- This line DO NOT provokes the warning
                    //showAnotherView = true
                } label: {
                    Text("Show")
                }
            }
            .padding(.trailing, 20)
            Spacer()
            Text("Main view")
            Spacer()

        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(.white)
        // 1.- This line provokes the warning
        .fullScreenCover(isPresented: $dismissWarningVM.showAnotherView) {
        // 2.- This line DO NOT provokes the warning
        //.fullScreenCover(isPresented: $showAnotherView) {
            AnotherView()
        }
    }
}

struct AnotherView: View {
    @Environment(\.dismiss) var dismiss

    var body: some View {
        VStack(spacing: 30) {
            Text("Another view")
            Button {
                dismiss()
            } label: {
                Text("Dismiss")
                    .foregroundColor(.red)
            }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .ignoresSafeArea()
    }
}

Can anyone help me?

Thanks!

1      

@Bnerd  

tried both scenarios, no console messages. Xcode 14.1, IOS 16.1

1      

Hi!

Thank you for your answer. I was trying with XCode 14.0.1 and getting the warning but after updating to XCode 14.1 the warning dissappear.

Thank you!

1      

After 14.1 the complier libray mark all @StateObject with @MainActor if you added this to the class then you would not got the warning

@MainActor
class NewObject: ObservableObject {

}

or

@MainActor @StateObject var newObject = NewObject()

1      

@NigelGee I tried adding @MainActor to the view model class but still get the warning. When I get some time I'll try to install again the version 14.0.1 and try "@MainActor @StateObject var newObject = NewObject()"

Thanks!

1      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.