gpt4 book ai didi

SwiftUI CoreData 崩溃预览

转载 作者:行者123 更新时间:2023-12-02 12:40:05 24 4
gpt4 key购买 nike

我有以下代码来绘制汽车列表,数据存储在 coredata 中。

但是,当我添加从数据库获取数据的代码行时,swiftui 预览似乎会中断。

错误日志显示以下内容:

PotentialCrashError: test app.app may have crashed

mileage app.app may have crashed. Check ~/Library/Logs/DiagnosticReports for any crash logs from your application.

==================================

| Error Domain=com.apple.dt.ultraviolet.service Code=12 "Rendering service was interrupted" UserInfo={NSLocalizedDescription=Rendering service was interrupted}

这是 foreach 开始和结束部分导致错误的代码:

import SwiftUI

struct CarListView: View {

@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(fetchRequest: Car.all()) var cars: FetchedResults<Car>

var body: some View {

NavigationView {
ZStack {
List {
Section(header: Text("Cars")) {
ForEach(self.cars, id: \.numberPlate) { car in
HStack {
VStack(alignment: .leading) {
Text(car.name)
Text(car.numberPlate)
}
}
}
}
}
}
}
}

}

struct CarListView_Previews: PreviewProvider {
static var previews: some View {
CarListView()
}
}

最佳答案

该问题似乎与它无法以某种方式获取允许在预览模式下获取数据的上下文有关。通过在预览模式下手动执行此操作,可以解决该问题。

struct CarListView_Previews: PreviewProvider {
static var previews: some View {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
return CarListView().environment(\.managedObjectContext, context)

}
}

关于SwiftUI CoreData 崩溃预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59514875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com