致命错误:从 Core Data/NSFetchedResultsController 删除对象时 UnsafeRawBufferPointer 的计数为负数

问题描述 投票:0回答:2

这是我第一次使用 AppKit 和

NSTableView
。它由核心数据
NSFetchedResultsController
支持。当数据集发生更改时,会发送
Notification

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)

这对于插入和更新数据非常有用。当我尝试删除某些内容时,我得到:

Thread 1: Fatal error: UnsafeRawBufferPointer with negative count

删除选定对象的代码如下:

// 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)
    }
}

感谢您提前提供的任何帮助!

swift macos nsfetchedresultscontroller combine appkit
2个回答
4
投票

error when i worked in my project

我是如何解决这个问题的

step 1 : copy name of your coredata(if you dont know that, it is with

step 2 : go to DataController file(if you dont know what is DataController it is used to connect swift ui with coredata , you can refer more from hacking with swift if you want)step 3: check name both names are same (in my case its different so its showing error:Error fixed in my case and buided successfully , this may help you


0
投票

就我而言,我只是忘记在文件顶部导入 CoreData 框架。

import CoreData
© www.soinside.com 2019 - 2024. All rights reserved.