我刚开始理解为什么 managedObjectContext 中的任何更新都会关闭视图。
我有这样的看法:
ConventView -> DetailView -> (SomeView1 & SomeView2)
和我的 SomeView2 波纹管:
import SwiftUI
struct ActInvList: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(sortDescriptors: [
SortDescriptor(\ActualItems.id),
]) var items: FetchedResults<ActualItems>
var body: some View {
Form{
Section(header: Text("Actual Inventory List")) {
HStack {
Text("#")
.frame(width:30)
Text("| ")
Text("Name")
Spacer()
Text("vol | wt")
}
.foregroundColor(Color.gray)
List {
ForEach(items, id: \.id) {item in
HStack {
Text(item.id ?? "")
.frame(width:30)
Text("| ")
Text(item.name ?? "")
Spacer()
Text("0 | 0")
}
}.onDelete(perform: deleteItems)
}
}
}
.navigationTitle("Actual Inventory List")
}
func deleteItems(at offsets: IndexSet) {
for offset in offsets {
let items = items[offset]
moc.delete(items)
}
try? moc.save()
}
}
当我进入 SomeView2 并删除(或添加)1 个项目时,它会将我从 SomeView2 抛出到 DetailView,但是当我回到 SomeView2 时,一切都变得正常