Xcode 11.4 issue: Context in environment is not connected to a persistent store coordinator

Viewed 616

Code was working in Xcode 11.3 but after updating to 11.4, I am now getting two errors:

Context in environment is not connected to a persistent store coordinator.

Thread 1: Fatal error: No ObservableObject of type UpdateTagView found. A View.environmentObject(_:) for UpdateTagView may be missing as an ancestor of this view.

This is how I add the Objects to the Environment:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        context.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy

        let em = EventManager()
        let tagger = UpdateTagView()

        let contentView = ContentView()
            .environment(\.managedObjectContext, context)
            .environmentObject(em)
            .environmentObject(tagger)

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }

I call them inside the View with @EnvironmentObject:

struct ViewName: View {

    @EnvironmentObject var em: EventManager
    @EnvironmentObject var tagger: UpdateTagView

Here is an example of what is in the ObservableObject:

class UpdateTagView: ObservableObject {

    @Published var counter: Int = 0
    @Published var language: String = "English"

}

Cheers~

1 Answers

I observed exactly the same issue when compiling our app in Release configuration. In Xcode 11.3 it worked but when we moved to Xcode 11.4 it stopped with exactly the same crash (@EnvironmentObject was not propagated in the hierarchy)

It looks like a bug in Swift compiler in metadata stripping algorithm. Temporarily you can either switch configuration to debug or manually switch Reflection Metadata Level (in your target Build Settings) from None to Without Names.

Related