Thursday, May 30, 2024
HomeiOS Developmentios - SwiftUI crashing when deleting merchandise in NavigationSpiltView

ios – SwiftUI crashing when deleting merchandise in NavigationSpiltView


I’ve a NavigationSplitView the place I checklist objects in information storage. If I delete the chosen one (the one that’s displaying particulars within the “element” a part of the navigationsplitview, the app crashes. If I delete one that’s not chosen for the time being, there isn’t any crash. For the deletion I exploit the .onDelete occasion within the Listing contained in the NavigationSplitView.

var physique: some View {
        @Bindable var nc = navigationContext
        NavigationSplitView {
//            let _ = print("Beginning NavigationSplitView")
            Listing(choice: $nc.selectedTimeline) {
//                let _ = print("Beginning Listing")
                ForEach(timelines){ timeline in
                    NavigationLink(timeline.title, worth: timeline)
                }
                .onDelete(carry out: removeTimelines)
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    EditButton()
                }
                ToolbarItem {
                    Button(motion: {showNewTimeline = true}) {
                        Label("Add Timeline", systemImage: "plus")
                    }
                }
                ToolbarItem {
                    Button(motion: {showEditTimeline = true}) {
                        Label("Edit Timeline", systemImage: "pencil")
                    }
                }
            }
        } element: {
            TimelineView(timeline: nc.selectedTimeline)
                .surroundings(navigationContext)
        }
        .sheet(isPresented: $showNewTimeline) {
            TimelineEditorView(timeline: nil)
        }
        .sheet(isPresented: $showEditTimeline) {
            TimelineEditorView(timeline: navigationContext.selectedTimeline)
        }
    }
    
    func removeTimelines(at indexSet: IndexSet) {
        print("Deleting timelines...")
        for index in indexSet {
            let timelineToDelete = timelines[index]
            if navigationContext.selectedTimeline?.persistentModelID == timelineToDelete.persistentModelID {
               navigationContext.selectedTimeline = nil
            }
            modelContext.delete(timelineToDelete)
        }
    }

The error is that this:
*** Assertion failure in -[SwiftUI.UpdateCoalescingCollectionView validateSelectionOrHighlightIndexPath:isSelection:allowMassIndexPath:assertIfInvalid:], UICollectionView.m:8266
Tried to change the number of an out-of-bounds merchandise (1) when there are just one objects in part 0. Assortment view: <SwiftUI.UpdateCoalescingCollectionView: 0x13480e600; baseClass = UICollectionView; body = (0 0; 420 998); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600001898390>; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <CALayer: 0x6000016949c0>; contentOffset: {0, -70}; contentSize: {420, 54}; adjustedContentInset: {70, 0, 0, 0}; format: <UICollectionViewCompositionalLayout: 0x133e15090>; dataSource: <TtGC7SwiftUI31UICollectionViewListCoordinatorGVS_28CollectionViewListDataSourceC12Timeline_iOS8Timeline_GOS_19SelectionManagerBoxS3
_: 0x135870160>>.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments