title: SwiftUI CoreData not saving / not detecting changes
body: <p>I have an entity called Cart, but it seems I can't get it to save a new data from my viewmodel.</p>
  <p>Persistence.swift</p>
  <pre><code>struct PersistenceController {
      static let shared = PersistenceController()
  
      static var preview: PersistenceController = {
          let result = PersistenceController(inMemory: true)
          let viewContext = result.container.viewContext
          for _ in 0..&lt;10 {
              let newItem = Item(context: viewContext)
              newItem.timestamp = Date()
          }
          for _ in 0..&lt;10 {
              let newCart = Cart(context: viewContext)
              newCart.menuName = &quot;name&quot;
              newCart.menuImg = &quot;img&quot;
              newCart.menuCode = &quot;code&quot;
          }
          do {
              try viewContext.save()
          } catch {
              let nsError = error as NSError
              fatalError(&quot;Unresolved error \(nsError), \(nsError.userInfo)&quot;)
          }
          return result
      }()
  
      let container: NSPersistentContainer
  
      init(inMemory: Bool = false) {
          container = NSPersistentContainer(name: &quot;Project&quot;)
          if inMemory {
              container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: &quot;/dev/null&quot;)
          }
          container.loadPersistentStores(completionHandler: { (storeDescription, error) in
              if let error = error as NSError? {
                  fatalError(&quot;Unresolved error \(error), \(error.userInfo)&quot;)
              }
          })
      }
      
      func save() {
          let context = container.viewContext
  
          if context.hasChanges {
              print(&quot;Found Changes&quot;)
              do {
                  try context.save()
              } catch {
                  print(&quot;ERROR&quot;)
                  let nserror = error as NSError
                  fatalError(&quot;Unresolved error \(nserror), \(nserror.userInfo)&quot;)
              }
          } else {
              print(&quot;NO Changes&quot;)
          }
      }
  }
  </code></pre>
  <p>MenuViewModel.swift</p>
  <pre><code>class MenuViewModel.swift: ObservableObject, MenuDetailService {
      @Environment(\.managedObjectContext) var managedObjectContext
  
      func addToCart() {
          let newCart = Cart(context: managedObjectContext)
          newCart.menuCode = menuCode
          newCart.menuName = menuName
          newCart.menuImg = menuImg
          PersistenceController.shared.save()
      }
  }
  </code></pre>
  <p>ProjectApp.swift</p>
  <pre><code>@main
  struct ProjectApp: App {
      @Environment(\.scenePhase) var scenePhase
  
      let persistenceController = PersistenceController.shared
  
      var body: some Scene {
          WindowGroup {
              ContentView()
                  .environment(\.managedObjectContext, persistenceController.container.viewContext)
          }.onChange(of: scenePhase) { _ in
              persistenceController.save()
          }
      }
  }
  </code></pre>
  <p>The persistence controller func save() always print no changes, seems like I use different context? and I don't know how to use the context in the persistence controller to save the new data. How do I do this? Thank you very much.</p>