BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

[Bug?] App Craches when using Destructive Button in SwipeActions In Child View

Forums > SwiftUI

Using SectionedFetchRequest to show each item per section in the Child view.

The problem occurs when

  1. the ChildView has only one item in one section.
  2. when deleting that item with a Destructive Button in a SwipeAction, the app crashes with the message "Thread 1: EXC_BREAKPOINT".
  3. 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)

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.