Core data SwiftUI中的手动核心数据请求
鉴于Core data SwiftUI中的手动核心数据请求,core-data,swiftui,Core Data,Swiftui,鉴于@FetchRequest不支持动态谓词(例如,我无法更新日历中的“月”并重新查询我在该月安排的事件),我正在尝试手动请求工作 如果我不尝试创建动态谓词,@FetchRequest已经可以工作了,因此我假设核心数据已配置为正常工作 在我的SceneDelegate中,我有: guard let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewConte
@FetchRequest
不支持动态谓词(例如,我无法更新日历中的“月”并重新查询我在该月安排的事件),我正在尝试手动请求工作
如果我不尝试创建动态谓词,@FetchRequest
已经可以工作了,因此我假设核心数据已配置为正常工作
在我的SceneDelegate
中,我有:
guard let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext else {
fatalError("Unable to read managed object context.")
}
let contentView = ContentView().environment(\.managedObjectContext, context)
在我的AppDelegate
中:
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "Transactions")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
如果我尝试在我的ContentView
中执行手动请求,例如:
let employeesFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Employees")
do {
let employees = try managedObjectContext.fetch(employeesFetch) as! [Employees]
} catch {
fatalError("Failed to fetch employees: \(error)")
}
让employeesFetch=NSFetchRequest(entityName:“员工”)
做{
let employees=try managedObjectContext.fetch(employeesFetch)as![employees]
}抓住{
fatalError(“无法获取员工:\(错误)”)
}
此操作失败,因为'Exception NSException*“+entityForName:nil不是用于搜索实体名称'Employees'的合法NSPersistentStoreCoordinator”
我缺少什么配置?在我的回答中:我演示了如何非常轻松地进行手动抓取。本主题有很多内容