So I'm using the NSFetchedResultsController for an entity that is always increased by 10 or any number divisible by 10. The issue I'm having is that after the delegate methods are called for the NSFetchResultsController and the rows are inserted, the tableView will scroll to some random spot (usually towards the bottom of the table). I don't know if this is an issue with the heights of the cells (dynamic), or the fact that the cells contain images that need to be loaded, or if I'm handling the delegate methods wrong, or if I'm creating the objects wrong. Any help would be appreciated
Also, I'm using MoPub hence the mp_
NSFetchedResultsControllerDelegate Methods
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView.mp_beginUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case NSFetchedResultsChangeType.insert:
guard let insertIndexPath = newIndexPath else { return }
self.tableView.mp_insertRows(atIndexPaths: [insertIndexPath], with: .fade)
case NSFetchedResultsChangeType.delete:
guard let deleteIndexPath = indexPath else { return }
self.tableView.mp_deleteRows(atIndexPaths: [deleteIndexPath], with: .fade)
default:
break
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView.mp_endUpdates()
}
Inserting method
for index in 0..<posts.count {
let post = posts[index]
guard let _ = RecentPost.createInManagedObjectContext(context: context, post: post, sequence: index) else { continue }
}
The createInManagedObjectContext
is just a method I use to insert the new object, and the post.count
is always a dividend of 10