使用NSFetchedResultsController和CoreData,UITableView意外地与beginUpdates()/ endUpdates()/ performBatchUpdates()一起反弹 [英] UITableView unexpectedly bounces with beginUpdates()/endUpdates()/performBatchUpdates() using NSFetchedResultsController and CoreData
问题描述
UITableView
意外地以 beginUpdates()
/ endUpdates()$ c反弹当行数填充视图时,使用
NSFetchedResultsController
和CoreData在$ c> / performBatchUpdates()
中使用。
复制非常简单。
-从主从应用程序模板(带有CoreData)创建一个新项目。
-在情节提要中,删除 showDetail设置。 (我们不需要详细视图)
-在MasterViewController中,将segue func prepare()
替换为:
UITableView
unexpectedly bounces with beginUpdates()
/ endUpdates()
/ performBatchUpdates()
using NSFetchedResultsController
and CoreData when the number of rows fill the view.
It's pretty simple to reproduce.
- Create a new project from the Master-Detail App Template (with CoreData).
- In the storyboard, remove the "showDetail" segue. (we don't need the detail view)
- In MasterViewController, replace segue func prepare()
with :
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let event = fetchedResultsController.object(at: indexPath)
let timestamp = event.timestamp
event.timestamp = timestamp // The idea is to simply update the Event entity.
}
启动应用程序(在iOS设备或模拟器中),并向其中添加足够的行填充视图(在iPhone SE中为11行)。
向下滚动视图,然后选择任意行。该视图将快速上下跳动。
是一个错误,还是代码有问题?
Launch the app (in iOS devices or Simulators), and add enough rows to fill the view (in iPhone SE, it 11 rows). Scroll down the view, and select any row. The view WILL rapidly BOUNCE up and down. Is that a bug, or is there an issue with the code ?
推荐答案
好,我可能已经找到一个解决方案,请告诉我您的想法。
这个想法是在 performBatchUpdates
中处理插入/删除/移动
并留下更新
。
所以我创建了这个枚举和属性:
Ok, I might have found a solution, please tell me guys what you think.
The idea would be to process insert/delete/move
in performBatchUpdates
and leave update
out of it.
So I've created this enum and property:
enum FetchedResultsChange<Object> {
case insert(IndexPath)
case delete(IndexPath)
case move(IndexPath, IndexPath, Object)
}
var fetchedResultsChanges: [FetchedResultsChange<Event>] = []
而 controllerWillChangeContent
为空:
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {}
didChange
变为:
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
self.fetchedResultsChanges.append(.insert(newIndexPath!))
case .delete:
self.fetchedResultsChanges.append(.delete(indexPath!))
case .update:
configureCell(tableView.cellForRow(at: indexPath!)!, withEvent: anObject as! Event) // So this stays untouched.
case .move:
self.fetchedResultsChanges.append(.move(indexPath!, newIndexPath!, anObject as! Event))
}
}
而 controllerDidChangeContent
变为:
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
guard self.fetchedResultsChanges.count > 0 else { return }
tableView.performBatchUpdates({
repeat {
let change = self.fetchedResultsChanges.removeFirst()
switch change {
case .insert(let newIndexPath):
tableView.insertRows(at: [newIndexPath], with: .fade)
case .delete(let indexPath):
tableView.deleteRows(at: [indexPath], with: .fade)
case .move(let indexPath, let newIndexPath, let event):
configureCell(tableView.cellForRow(at: indexPath)!, withEvent: event)
tableView.moveRow(at: indexPath, to: newIndexPath)
}
} while self.fetchedResultsChanges.count > 0
}, completion: nil)
}
那么您怎么看?
这篇关于使用NSFetchedResultsController和CoreData,UITableView意外地与beginUpdates()/ endUpdates()/ performBatchUpdates()一起反弹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!