小编Gol*_*ver的帖子

SwiftUI/Core Data:在从 .onDelete 修饰符调用的托管对象上下文上使用 Save() 时发生崩溃

在我的 SwiftUI 应用程序中,我有一个 .onDelete SwiftUI 修饰符(在导航视图、列表、ForEach、链接等内),用于滑动删除核心数据实体对象。直到删除核心数据对象为止一切都很好。当我在托管对象上下文上调用 .Save() 时,发生崩溃。我一直在拔头发试图找出原因。这是代码的快照:

        List {
            ForEach(claims, id: \.self.id) {(tmpClaim: Claim) in
                NavigationLink(destination: EditClaimView(passedClaim: tmpClaim, uuidString: tmpClaim.id.uuidString)) {
                    HStack {
                        VStack(alignment: .leading, spacing: 5) {
                            Text(tmpClaim.providerName)
                                .font(.headline)
                            Text("\(tmpClaim.serviceDate, formatter: DateFormatter.editing)")
                                .font(.footnote)
                        }
                        Spacer()
                        Text("\(String(describing: tmpClaim.serviceAmount).currencyFormatting())")
                    }
                    .contextMenu {
                        Button(action:
                            {
                                // code to create a new claim is here
})
                            {
                                Text("Duplicate")
                                Image(systemName: "doc.on.doc")
                            }
                    }
                }
            }
            .onDelete { indexSet in
                for index in indexSet {
                    (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext.delete(self.claims[index])
                }
if (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext.hasChanges { …
Run Code Online (Sandbox Code Playgroud)

core-data ios swiftui swiftui-list

8
推荐指数
0
解决办法
460
查看次数

标签 统计

core-data ×1

ios ×1

swiftui ×1

swiftui-list ×1