I would like to add more items to my coreData model array in my tableView when the user scrolls to the top by executing further fetch requests.
The problem I'm having is when I finish the new fetchRequest and add that data to the model array and call reloadData, the tableView jumps to the top.
What I would like to achieve is what you see in any chat app when the user scrolls to the top. The new data is fetched and added to the tableView while the user scroll position remains unchanged and scrolling is not interrupted. Only the scroll bar can be seen to be getting smaller and now the user can scroll further up.
func loadAdditionalMessages() {
let request: NSFetchRequest<Message> = NSFetchRequest(entityName: "Message")
let messageSort = NSSortDescriptor(key: "messageDateCreated", ascending: false)
request.sortDescriptors = [messageSort]
request.predicate = NSPredicate(format: "messageRoomId == %@", self.messageRoomId!)
request.fetchLimit = 80
request.fetchOffset = self.fetchOffset
fetchOffset += 80
do {
let fetchedResults = try context.fetch(request)
self.messages += fetchedResults
tableView.reloadData()
} catch {
print("Could not fetch \(error)")
}
}