SwiftUI @FetchRequest 使应用程序崩溃并返回错误 [英] SwiftUI @FetchRequest crashes the app and returns error
问题描述
我正在尝试使用 Xcode 11 在我的 mac 应用程序中使用带有 SwiftUI 的核心数据.我在创建项目时检查了使用核心数据".我还创建了实体(称为 VisitedCases)并使用编辑器创建 NSManagedObject 子类文件.我还将 Codegen 设置为手动/无.下面是生成的 NSManagedObject 文件中的代码:
I am trying to use core data within my mac app with SwiftUI using Xcode 11. I have checked "Using Core Data" when creating the project. I also have created the entity (called VisitedCases) and used editor to create NSManagedObject Subclass files. I also have set the Codegen to Manual/none. Here is the code in the generated NSManagedObject files:
VisitedCases+CoreDataProperties.swift
extension VisitedCases {
@nonobjc public class func fetchRequest() -> NSFetchRequest<VisitedCases> {
return NSFetchRequest<VisitedCases>(entityName: "VisitedCases")
}
@NSManaged public var caseNumber: String
}
VisitedCases+CoreDataClass.swift
@objc(VisitedCases)
public class VisitedCases: NSManagedObject {
}
我将 ContentView.swift 中的@Environment 变量和@FetchRequest 称为:
I called the @Environment variable and the @FetchRequest in ContentView.swift as:
struct ContentView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: VisitedCases.entity(),
sortDescriptors: []
) var orders: FetchedResults<VisitedCases>
//@State vars and the rest of the code
}
但是,当我运行时,应用程序一启动就崩溃,输出中出现以下错误:
However, when I run, the app crashed as soon as launch with the following errors in the output:
2020-02-23 18:36:16.889306+0330 ImageSelector[17874:149503] [error] error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'VisitedCases' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?
CoreData: error: No NSEntityDescriptions in any model claim the NSManagedObject subclass 'VisitedCases' so +entity is confused. Have you loaded your NSManagedObjectModel yet ?
2020-02-23 18:36:16.889389+0330 ImageSelector[17874:149503] [error] error: +[VisitedCases entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
CoreData: error: +[VisitedCases entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
2020-02-23 18:36:16.921131+0330 ImageSelector[17874:149503] executeFetchRequest:error: A fetch request must have an entity.
我还有一个函数可以将字符串保存到存储中,并且似乎工作得很好:
I also have a function that saves a string to storage and seems to work just fine:
func addCaseNumber (caseNo: String) {
guard caseNo != "" else {return}
let newCaseNumber = VisitedCases(context: self.managedObjectContext)
newCaseNumber.caseNumber = caseNo
do {
try self.managedObjectContext.save()
print("Case number saved.")
} catch {
print(error.localizedDescription)
}
}
我的代码有什么问题,我应该怎么做才能修复它?
What is wrong with my code and what should I do to fix it?
推荐答案
在@enviromentVAr中,有时需要自己设置环境.
In the @enviromentVAr, sometimes, you need to set the enviroment by yourself.
let managedObjectContext: NSManagedObjectContext = ((UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext)!
ContentView().environment(\.managedObjectContext, managedObjectContext)
那么 managedObjectContext 就可以工作了.
Then the managedObjectContext can work.
@Environment(\.managedObjectContext) var managedObjectContext
这篇关于SwiftUI @FetchRequest 使应用程序崩溃并返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!