Xcode macOS Document App
使用模板创建项目,Use Core Data
选中复选框。- 将 Book 实体添加到
Document.xcdatamodeld
- 将 FetchRequest 扭曲属性添加到
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 示例,但找不到。以下是一些类似或相关的问题。不幸的是,这个问题没有解决。
- 使用 @fetchRequest(entity:) 处理 SwiftUI macOS 应用程序崩溃
- SwiftUI @FetchRequest 使应用程序崩溃并返回错误
- https://developer.apple.com/forums/thread/124656?answerId=417869022#417869022
- https://developer.apple.com/forums/thread/132624
编辑:将此问题项目上传到 Github,https://github.com/donly/DocMacDemo。