UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

[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)

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.