这是我第一次使用 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)
}
}
感谢您提前提供的任何帮助!