Fatal error: UnsafeRawBufferPointer with negative count on deleting object(s) from Core Data/NSFetchedResultsController

Viewed 243

It is the first time for me working with AppKit and a NSTableView. It is backed by a NSFetchedResultsController for Core Data. When there are changes to the dataset, a Notification is being sent:

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
    guard let links = self.fetchedResultsController.fetchedObjects else {
        return
    }

    if self.viewContext.hasChanges {
        try? self.viewContext.save()
    }
        
    self._links = links
    NotificationCenter.default.post(name: .reloadLinkList, object: nil)
}
NotificationCenter.default.publisher(for: .reloadLinkList).receive(on: RunLoop.main).sink { notification in
    self.list.tableView.reloadData()
    self.list.linksModel.selectedRows = IndexSet([])
}
.store(in: &cancellables)

This works great for inserting and updating data. When I try to delete something, I get:

Thread 1: Fatal error: UnsafeRawBufferPointer with negative count

The code for deleting selected objects looks as following:

// selector methode for UIMenuItem`s action
@objc func onDeleteSelectedLinks(_ sender: AnyObject) {
    list.linksModel.deleteLinks(links: selectedLinks)
}
// Method for deleting links from the view context
func deleteLinks(links: [LBLink]) {
    for link in links {
        self.viewContext.delete(link)
    }
}

Thank you for any help in advance!

0 Answers
Related