Using SectionedFetchRequest to show each item per section in the Child view.
The problem occurs when
- the ChildView has only one item in one section.
- when deleting that item with a Destructive Button in a SwipeAction, the app crashes with the message "Thread 1: EXC_BREAKPOINT".
- there is nothing in the debugging.
I googled the issue and found that removing the Button's role:.destructive fixes the problem.
struct MainCatalogDetailView: View {
@Environment(\.managedObjectContext) private var moc
@SectionedFetchRequest<String, ItemEntity> private var items: SectionedFetchResults<String, ItemEntity>
private let filter: FilterType
private let name: String
private let catalog: Catalog
init(filter: FilterType, name: String, sectionIdentifier: KeyPath<ItemEntity, String>, sortDescriptors:[SortDescriptor<ItemEntity>], catalog: Catalog) {
self.filter = filter
self.name = name
self.catalog = catalog
_items = SectionedFetchRequest<String, ItemEntity>(sectionIdentifier: sectionIdentifier,
sortDescriptors: sortDescriptors,
predicate: NSPredicate(format: "%K == %@", catalog.filterKey, name))
}
@State private var selectedItem: ItemEntity? = nil
@State private var selectedItemToEdit: ItemEntity? = nil
@State private var showingAddView = false
@State private var searchText = ""
var body: some View {
List {
ForEach(items) { category in
Section {
ForEach(category) { item in
MainListRowView(item: item)
.swipeActions(edge: .trailing) {
Button { // here
delete(item: item)
} label: {
Label("Delete", systemImage: "trash")
}
.tint(.red)
Button {
selectedItemToEdit = item
} label: {
Label("Edit", systemImage: "square.and.pencil")
}
.tint(.orange)
}
.onTapGesture {
selectedItem = item
}
}
} header: {
headerView(category: category.id, count: category.count)
.textCase(nil)
}
.listRowInsets(EdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10))
.listRowSeparatorTint(.strokeColor)
.listSectionSeparator(.hidden)
}
}
.listStyle(.sidebar)