轻松处理NSFetchedResultsController
在UITableView
或UICollectionView
中的更新。
这是为什么?
CoreData有一个叫做NSFetchedResultsController
的优秀工具。这个工具允许在NSManagedObjectContext
中创建或删除符合特定要求(NSPredicate
)的对象时接收通知。每当这些对象创建或删除时,您可以告诉相应的UITableView
或UICollectionView
相应地插入/删除行。
对于UITableView
来说,这相对更容易一些(因为Apple提供了一个CoreData Master/Detail项目模板,它包含了完成此操作的代码)。
对于UICollectionView
来说,则稍微麻烦一些(因为分组更新是通过处理程序而不是使用beginUpdates
/endUpdates
方法进行的)。
本项目使得在CoreData项目中使用NSFetchedResultsController
类更加简便(对于UITableView
来说,操作会更加简单):直接接收结果控制器的通知并将其转发到您的表格视图或集合视图。
使用示例
/* *** Collection View Setup *** */
/* First, how to handle a reload of a cell? */
collectionView.fetchedResultsControllerReloadMode = .handler({ [weak self] cell, object, collectionViewIndexPath, dataSourceIndexPath in
self?.setup(cell: cell as! MyCellType, with: object as! MyObjectType)
})
/* Next, when a cell moves, what to do? Here, we do a move (other solution
* is to delete/insert the cell).
* When a cell is moved, it must be reloaded too. We say we want the
* standard reload mode (the one we have defined above). */
collectionView.fetchedResultsControllerMoveMode = .move(reloadMode: .standard)
/* *** Handling the NSFetchedResultsController notifications *** */
/* We simply forward the methods to the collection view */
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
collectionView.fetchedResultsControllerWillChangeContent()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
collectionView.fetchedResultsControllerDidChange(section: sectionInfo, atIndex: sectionIndex, forChangeType: type)
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
collectionView.fetchedResultsControllerDidChange(object: anObject, atIndexPath: indexPath, forChangeType: type, newIndexPath: newIndexPath)
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
collectionView.fetchedResultsControllerDidChangeContent(endUpdatesHandler: nil)
}
安装
项目与Carthage兼容。
您也可以简单地复制项目中的两个UICollectionView
扩展和UITableView
扩展到您的项目中。
鸣谢
该项目的最初版本是在François Lamboley在happn工作时创建的,他的GitHub地址是https://github.com/Frizlab。