SwiftUI 中新 window 的上下文环境

Context environment to new window in SwiftUI

这两天我尝试用

解决众所周知的问题
Context in environment is not connected to a persistent store coordinator: <NSManagedObjectContext: ...> 

在我的应用程序中。

我尝试了几乎所有在旧 google 上找到的已发布解决方案 - 即使我试图了解日语网站。我把线

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

几乎每一行代码:错误或无结果。

我的问题:我喜欢打开一个新的 window 并显示一些东西的列表(这里:地址)。必须有一个简单的解决方案,无需重写和扩展框架的某些部分。在我看来,这不是一件很特别的事情,因此苹果可能有一个简单的解决方案,我看不到。

到目前为止我的代码:

XXXXApp.swift


    import SwiftUI

    @main  <<-- here: Context in environment is not connected to a persis....
    struct XXXXApp: App {
        let persistenceController = PersistenceController.shared

        var body: some Scene {
            WindowGroup {
                ContentView()
                    .environment(\.managedObjectContext, persistenceController.container.viewContext)
                    .navigationTitle("Buchliste")
            }
            WindowGroup("AddressBook") {
                AddressBookView()
                    .environment(\.managedObjectContext, persistenceController.container.viewContext)
                    .navigationTitle("Adressbuch")
            }.windowToolbarStyle(UnifiedCompactWindowToolbarStyle()).windowStyle(HiddenTitleBarWindowStyle())
            Settings {
                PreferencePane()
            }
        }
    }

    class WindowController<RootView: View>: NSWindowController {
        let persistenceController = PersistenceController.shared
        convenience init(rootView: RootView, width: Int, height: Int) {
            let hostingController = NSHostingController(rootView: rootView.frame(width: CGFloat(width), height: CGFloat(height)))
            let window = NSWindow(contentViewController: hostingController)   <<-- here: Context in environment is not connected to a persis....
            window.setContentSize(NSSize(width: width, height: height))
            self.init(window: window)
        }
    }


ContextView.swift 中的片段:


    ToolbarItem(placement: .automatic) {
        Button {
              let windowController = WindowController(rootView: AddressBookView(), width: 800, height: 600 )
              windowController.window?.title = "Adressbuch"   <<-- here: Context in environment is not connected to a persis....
              windowController.showWindow(nil)
           } label: {
           Label("Adressbuch", systemImage: "book")
        }
    }


所以,我的错误在哪里?我必须在哪里放置哪些代码才能正确制作应用 运行?

HEUREKA!!

class WindowController<RootView: View>: NSWindowController {
    convenience init(rootView: RootView, width: Int, height: Int) {
        let persistenceController = PersistenceController.shared
        let hostingController = NSHostingController(rootView: rootView.frame(width: CGFloat(width), height: CGFloat(height)).environment(\.managedObjectContext, persistenceController.container.viewContext))
        let window = NSWindow(contentViewController: hostingController)
        window.setContentSize(NSSize(width: width, height: height))
        self.init(window: window)
    }
}

这是 WindowController 的正确代码。 environment-bla 必须作为参数添加到 frame 之后。将 persistenceController 放入 init 中,我们开始吧!