在预览中获取数据:“提取请求必须具有一个实体uncaughtexceptionError”。
我有一个listView文件,可以在没有核心数据的情况下构建预览代码罚款。但是,当我在ContentView中构建该listView时,我会收到一个错误,该错误在文本编辑器中没有像通常一样,而是在诊断窗口中显示。错误是“提取请求必须具有一个实体uncaughtexceptionError:[appName]由于未知的例外而坠毁”。
当我在模拟器中运行应用程序时,ContentView似乎构建正常。 (text(“+”)附加的函数
也不起作用,关于“映射表参数为null”,但一次错误。)
编辑:核心数据中的实体是设置为“类定义”。
非常感谢。
struct ListView: View {
@Environment(\.managedObjectContext) var viewContext
@FetchRequest(sortDescriptors: []) var targets: FetchedResults<TargetEntity>
@FetchRequest(sortDescriptors: []) var positives: FetchedResults<PositiveEntity>
var body: some View {
VStack {
HStack {
VStack {
Text("+")
.onAppear{add()}
.onTapGesture (count: 2){
do {
print("error")
increment(targets.first!)
try viewContext.save()
} catch {
print("error")
}
}
.onLongPressGesture {
addPositive()
}
List{
ForEach(positives) { item in
Text(item.title ?? "Unknown Title")
}
.onDelete(perform: deleteItems)
}
}
}
}
}
func add() {
let countnum = TargetEntity(context: viewContext)
countnum.countnum = 0
save()
}
func addPositive(){
let newPositive = PositiveEntity(context: viewContext)
newPositive.title = "Action"
save()
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { positives[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
func save() {
do { try viewContext.save() } catch { print(error) }
}
}
struct ListView_Previews: PreviewProvider {
static var previews: some View {
NavigationView{
ListView()//.environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}
}
I have a ListView file which can build the preview code fine without the Core Data piece in it. However, when I build that ListView in the ContentView, I got an error that is not shown in the text editor like usual but in the diagnostics window. The error is "A fetch request must have an entity UncaughtExceptionError: [AppName] crashed due to an uncaught exception".
When I run the app in the simulator, the ContentView seems to build fine. (the functions attached to Text("+")
don't work either, something about "map table argument is NULL", but one error at a time.)
EDIT: The entities in Core Data are set up as “Class Definition”.
Many many thanks in advance.
struct ListView: View {
@Environment(\.managedObjectContext) var viewContext
@FetchRequest(sortDescriptors: []) var targets: FetchedResults<TargetEntity>
@FetchRequest(sortDescriptors: []) var positives: FetchedResults<PositiveEntity>
var body: some View {
VStack {
HStack {
VStack {
Text("+")
.onAppear{add()}
.onTapGesture (count: 2){
do {
print("error")
increment(targets.first!)
try viewContext.save()
} catch {
print("error")
}
}
.onLongPressGesture {
addPositive()
}
List{
ForEach(positives) { item in
Text(item.title ?? "Unknown Title")
}
.onDelete(perform: deleteItems)
}
}
}
}
}
func add() {
let countnum = TargetEntity(context: viewContext)
countnum.countnum = 0
save()
}
func addPositive(){
let newPositive = PositiveEntity(context: viewContext)
newPositive.title = "Action"
save()
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { positives[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
func save() {
do { try viewContext.save() } catch { print(error) }
}
}
struct ListView_Previews: PreviewProvider {
static var previews: some View {
NavigationView{
ListView()//.environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的猜测是,您的
@fetchrequest
s没有返回任何实体。通常这是可以的。当您运行该应用程序时,没关系。但是预览出于某种原因崩溃,我相信这是一个错误。
您甚至可以通过创建新的Swiftui应用程序并选择Core Data(复选框)来重现此错误。
如果您进入持久性。Swift文件并评论其模拟数据生成(第11-21行)。就像您的内容一样,他们对ContentView的预览也将崩溃。
我对此表示反馈,以告诉苹果有关该问题的问题(FB:FB12008143)
作为测试
,添加一些模拟数据,以便从两个提取请求中返回某些东西。这对我有用(尽管这并非一直以来都是理想的)。
My guess is that one of your
@FetchRequest
s are not returning any entities.Normally this is ok. And when you run the app, it's ok. But Preview crashes for some reason and I believe this is a bug.
You can even reproduce this bug by creating a new SwiftUI app and selecting Core Data (checkbox).
If you go into the Persistence.swift file and comment out their mock data generation (lines 11-21). Their preview for ContentView will also crash, just like yours did.
I opened up a Feedback for this to tell Apple about the problem (FB: FB12008143)
Workaround
As a test, add some mock data so something is returned from both fetch requests. This works for me (though it's not ideal all the time).
在用核心数据创建的项目中,在使用新实体更新核心数据模型之后,就面临同一问题。
错误仅在“帆布”预览中发生,使用模拟器或真实设备都可以。
通过将创建模型的模拟添加到
persistenceController
preview var下,为我解决了问题。Just faced same issue after updating core data model with new entity, in a project created with core data.
Error happens only to "canvas" preview, with simulator or real devices everything is ok.
Issue resolved for me by adding mock of created model to
PersistenceController
under thepreview
var只需使用Xcode 14.3.1遇到此问题。
通过添加实体var(用户)预览来解决。
}
Just run into this issue with Xcode 14.3.1.
Solved by adding an entity var (user) to preview.
}