title: CoreData and SwiftUI: Context in environment is not connected to a persistent store coordinator
body: <p>I am trying to teach myself Core Data by building a homework managing app. My code builds fine and the app runs okay until I try to add a new assignment to the list. I get this error <code>Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1c25719e8)</code> on the following line: <code>ForEach(courses, id: \.self) { course in</code>. The console also has this error: <code>Context in environment is not connected to a persistent store coordinator: &lt;NSManagedObjectContext: 0x2823cb3a0&gt;</code>.</p>
  
  <p>I know very little about Core Data and am at a loss as to what the issue might be. I have set up "Assignment" and "Course" entities in the data model, where Course has a one-to-many relationship to Assignment. Each assignment will be categorized under a particular course.</p>
  
  <p>This is the code for the view that adds a new assignment to the list:</p>
  
  <pre><code>    struct NewAssignmentView: View {
  
      @Environment(\.presentationMode) var presentationMode
      @Environment(\.managedObjectContext) var moc
      @FetchRequest(entity: Course.entity(), sortDescriptors: []) var courses: FetchedResults&lt;Course&gt;
  
      @State var name = ""
      @State var hasDueDate = false
      @State var dueDate = Date()
      @State var course = Course()
  
      var body: some View {
          NavigationView {
              Form {
                  TextField("Assignment Name", text: $name)
                  Section {
                      Picker("Course", selection: $course) {
                          ForEach(courses, id: \.self) { course in
                              Text("\(course.name ?? "")").foregroundColor(course.color)
                          }
                      }
                  }
                  Section {
                      Toggle(isOn: $hasDueDate.animation()) {
                          Text("Due Date")
                      }
                      if hasDueDate {
                          DatePicker(selection: $dueDate, displayedComponents: .date, label: { Text("Set Date:") })
                      }
                  }
              }
              .navigationBarTitle("New Assignment", displayMode: .inline)
              .navigationBarItems(leading: Button(action: {
                  self.presentationMode.wrappedValue.dismiss()
              }, label: { Text("Cancel") }),
                                  trailing: Button(action: {
                                      let newAssignment = Assignment(context: self.moc)
                                      newAssignment.name = self.name
                                      newAssignment.hasDueDate = self.hasDueDate
                                      newAssignment.dueDate = self.dueDate
                                      newAssignment.statusString = Status.incomplete.rawValue
                                      newAssignment.course = self.course
                                      self.presentationMode.wrappedValue.dismiss()
                                  }, label: { Text("Add").bold() }))
          }
      }
  }
  </code></pre>
  
  <p>EDIT: Here is the code in AppDelegate that sets up the persistent container:</p>
  
  <pre><code>lazy var persistentContainer: NSPersistentCloudKitContainer = {
      let container = NSPersistentCloudKitContainer(name: "test")
      container.loadPersistentStores(completionHandler: { (storeDescription, error) in
          if let error = error as NSError? {
              fatalError("Unresolved error \(error), \(error.userInfo)")
          }
      })
      return container
  }()
  </code></pre>
  
  <p>And the code in SceneDelegate that sets up the environment:</p>
  
  <pre><code>    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
      // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
      // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
      // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
  
      // Get the managed object context from the shared persistent container.
      let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
  
      // Create the SwiftUI view and set the context as the value for the managedObjectContext environment keyPath.
      // Add `@Environment(\.managedObjectContext)` in the views that will need the context.
      let contentView = ContentView().environment(\.managedObjectContext, context)
  
      // Use a UIHostingController as window root view controller.
      if let windowScene = scene as? UIWindowScene {
          let window = UIWindow(windowScene: windowScene)
          window.rootViewController = UIHostingController(rootView: contentView)
          self.window = window
          window.makeKeyAndVisible()
      }
  }
  </code></pre>