iOS 14 Beta - Core Data in Swift UI Error: No NSEntityDescriptions in any model claim the NSManagedObject subclass

iOS 14 Beta - Core Data in Swift UI Error: No NSEntityDescriptions in any model claim the NSManagedObject subclass

我正在使用新的 SwiftUI App 协议并通过@Environment 传递我的核心数据 NSPersistentContainer:

        final class PersistentStore: ObservableObject {
            static let shared = PersistentStore()
            let persistentContainer: NSPersistentContainer
            private init() {
                    self.persistentContainer = {
                        let container = NSPersistentContainer(name: "SillyModel")
                        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
                            if let error = error as NSError? {
                                fatalError("Unresolved error \(error), \(error.userInfo)")
                            }
                        })
                        return container
                    }()
            }
            func saveContext() {
                let context = persistentContainer.viewContext
                if context.hasChanges {
                    do {
                        try context.save()
                    } catch {
                        let nserror = error as NSError
                        fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
                    }
                }
            }
    }

    
    @main
    struct SillyApp: App {
        
        @Environment(\.scenePhase) private var scenePhase
        
        var body: some Scene {
            WindowGroup {
                NavigationView {
                    ProjectGridView()
                }
                .environment(\.managedObjectContext, PersistentStore.shared.persistentContainer.viewContext)
            }
        }
}

然后我在 ProjectGridView 中使用 @Enviroment 从环境中获取 managedObjectContect,尝试 @FetchRequest 并显示一个新的 LazyVGrid:

struct ProjectGridView: View {
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(
      entity: Project.entity(),
      sortDescriptors: [
        NSSortDescriptor(keyPath: \Project.title, ascending: true)
      ]
    ) var projects: FetchedResults<Project>
    
    let columns = [GridItem(.adaptive(minimum: 80.0))]
    
    var body: some View {
        return ScrollView {
            LazyVGrid(columns: columns, spacing: 20) {
                ForEach(projects, id:\.title) { (project: Project) in
                    Text("\(project.title ?? "")")
                }
            }
            .padding(.horizontal)
        }
    }
} 

我收到这个错误:

[error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'Project' so +entity is confused.  Have you loaded your NSManagedObjectModel yet ?

我试过编写保存新 Project() 的代码,甚至在其他地方使用 @FetchRequest。我猜它可能与 LazyVGrid 的“惰性”部分有关?有什么想法吗?

好的,已从 Apple Developer 论坛找到修复程序。这似乎只是一个测试版错误:

要修复,请为您的模型创建一个扩展 class:

import Foundation
import CoreData

extension Project {
    class func swiftUIFetchRequest() -> NSFetchRequest<Project> {
        let request = NSFetchRequest<Project>(entityName: "Project")
        request.sortDescriptors = [NSSortDescriptor(keyPath: \Project.title, ascending: true)] /* You can add custom sort descriptors here */
        return request
    }
}

然后像这样调用@FetchRequest:

@FetchRequest(fetchRequest: Project.swiftUIFetchRequest()) var projects: FetchedResults<Project>

感谢 Apple 开发者论坛中解决此问题的人。