How to generate CoreData objects when pushing a button?

I have a simple app that generates a record based on some user input. I'd like to take it to a summary of that record after taking in the input and generating a report from that input. The way I've rigged it so far is that the button fires off actions that generate the record also fires off the following code that redirects the UI to the record view.


if let window = windowScene?.windows.first {
    window.rootViewController = UIHostingController(rootView: ReadingSpreadView(history: historyEntry, spread: spread, spreadTitles: spreadTitles))
    window.makeKeyAndVisible()
}

However, when I hit my custom back button my MOC context is being lost somewhere as I'm getting:

Context in environment is not connected to a persistent store coordinator: <NSManagedObjectContext: 0x30398c4e0>

When I try to browse the history view.

I suspect the reason why I need the custom back button and that I'm losing the MOC context is that I'm doing something wrong with CoreData, or SwiftUI or something.

What's the appropriate way of doing this? Most of my experience has been in MVC frameworks on the Web with JavaScript frontend if that helps.

How to generate CoreData objects when pushing a button?
 
 
Q