TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Error: Context in environment is not connected to a persistent store coordinator

Forums > SwiftUI

I am getting the error referenced in the title, and I've tried a few different ways to fix it, but nothing seems to work. Any help would be much appreciated!

The error appears in my (appname).swift file right at the @main line. I've made sure all views have the @Environment(\.managedObjectContext) var managedObjectContext property, and I've tried passing the managedObjectContext through the view hierarchy.

This is what I currently have for my appname.swift file:

@main
struct devManageApp: App {
    let persistenceController = PersistenceController.shared

    @Environment(\.managedObjectContext) var managedObjectContext

    var body: some Scene {
        WindowGroup {
            LockScreen()
                .environment(\.managedObjectContext, self.managedObjectContext)

        }
    }
}

And in the intial view (removed some unrelated code for conciseness):

struct LockScreen: View {

    @State private var isUnlocked = false

    @Environment(\.managedObjectContext) var managedObjectContext

    var body: some View {
        NavigationView {
        VStack{
            Image(systemName: "faceid").resizable().frame(width: 100, height: 100, alignment: .center).onAppear {
                authenticate()
            }
            if isUnlocked {
                NavigationLink(destination: ClientList(managedObjectContext: _managedObjectContext)
                                .environment(\.managedObjectContext, self.managedObjectContext)
                )
                {
                    Text("Open")
                }
            } else {
                Text("Locked")
            }
        }
        }   
    }  
}

4      

You haven't injected a Managed Object Context into the environment yet so you can't read one out, like you do here:

@Environment(\.managedObjectContext) var managedObjectContext

You don't need this line.

Instead, you should pass the MOC from your PersistenceController into the environment of the LockScreen, like this:

.environment(\.managedObjectContext, persistenceController.container.viewContext)

3      

@roosterboy What an easy fix! I've learned so much from this community, and I really appreciate it. Thanks much!

4      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.