macOS Document App SwiftUI 项目上的 NSPersistentDocument FetchRequest 扭曲属性崩溃

Posted

技术标签:

【中文标题】macOS Document App SwiftUI 项目上的 NSPersistentDocument FetchRequest 扭曲属性崩溃【英文标题】:NSPersistentDocument FetchRequest warp property crash on macOS Document App SwiftUI project 【发布时间】:2020-07-03 14:31:56 【问题描述】:
    使用Xcode macOS Document App 模板创建项目,并选中Use Core Data 复选框。 将 Book 实体添加到 Document.xcdatamodeld 将 FetchRequest warp 属性添加到ContentView
@FetchRequest(entity: Book.entity(), sortDescriptors: []) var books: FetchedResults<Book>
    构建并运行,崩溃

来自控制台的崩溃日志是

2020-07-03 23:12:23.597880+0800 DocMacDemo[15236:4376209] [error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'DocMacDemo.Book' so +entity is confused.  Have you loaded your NSManagedObjectModel yet ?
CoreData: error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'DocMacDemo.Book' so +entity is confused.  Have you loaded your NSManagedObjectModel yet ?
2020-07-03 23:12:23.598287+0800 DocMacDemo[15236:4376209] [error] error: +[DocMacDemo.Book entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
CoreData: error: +[DocMacDemo.Book entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
2020-07-03 23:12:23.644491+0800 DocMacDemo[15236:4376209] executeFetchRequest:error: A fetch request must have an entity.
2020-07-03 23:12:23.653769+0800 DocMacDemo[15236:4376209] [error] error: The fetch request's entity 0x600003500420 'Book' appears to be from a different NSManagedObjectModel than this context's
CoreData: error: The fetch request's entity 0x600003500420 'Book' appears to be from a different NSManagedObjectModel than this context's
(lldb)

我已经找了几天 NSPersistentDocument SwiftUI 示例,但找不到。 以下是一些类似或相关的问题。不幸的是,这个问题没有解决。

Using @fetchRequest(entity: ) for SwiftUI macOS app crashes SwiftUI @FetchRequest crashes the app and returns error https://developer.apple.com/forums/thread/124656?answerId=417869022#417869022 https://developer.apple.com/forums/thread/132624

编辑: 将此问题项目上传到 Github,https://github.com/donly/DocMacDemo。

【问题讨论】:

【参考方案1】:

这是由于新文档为空。与任何基于文档的应用程序一样,您必须为新文档准备一些默认初始数据

这是可能的解决方案。使用 Xcode 11.4 / ios 13.4 测试

在 Document.swift 中

class Document: NSPersistentDocument 

    // .. other code here

    override func makeWindowControllers() 

        // in case of new document create new empty book in context
        // that will be shown in opened document window
        let isNew = self.fileURL == nil
        if isNew 
            _ = Book(context: self.managedObjectContext!)       // << here !!
        

        let contentView = ContentView().environment(\.managedObjectContext, self.managedObjectContext!)

        // ... other code here

【讨论】:

【参考方案2】:

嗯,Asperi 的答案很好,但是在对象模型中给你留下了一本可能不需要的书。另一种方法是将上下文保存在 Asperi 建议的相同位置:

    if let context = managedObjectContext 
        try? context.save()
    

那么FetchRequest也不会有错误信息。

【讨论】:

以上是关于macOS Document App SwiftUI 项目上的 NSPersistentDocument FetchRequest 扭曲属性崩溃的主要内容,如果未能解决你的问题,请参考以下文章

mac电脑中的macos.app文件如何运行

macOS:给 app 添加摄像头权限

macOS:给 app 添加摄像头权限

MacOS上界面精美的10款App推荐

EmacsMacPort (Emacs.app) 不加载我自己的,MacOS 中的 ~/.emacs

如何防止Electron app中断macOS系统关机?