2

I'm getting the following error after upgrading to iOS 14. I'm using a library called MessageKit that this is built with.

While preparing update a visible view at <NSIndexPath: 0xd92d17177ece800a> {length = 2, path = 25 - 0} wasn't found in the current data model and was not in an update animation

Does anyone know what this error means? The crash is pointing to the method scrollToBottom on the collection view. Looks like for whatever reason the last item in the model isn't there anymore when it's running an animation or something. I'm wondering if it could be due to the typing indicator, but I'm not sure.

func insertMessage(_ message: NewMessageModel) {
    // Reload last section to update header/footer labels and insert a new one
    DispatchQueue.main.async {
        
        self.isPerformingBatchUpdates = true
        self.messagesCollectionView.performBatchUpdates({
            self.messages.append(message)   //The messages object can only be modified on the main thread
            self.checkLastMessageSent(message: message)
            self.messagesCollectionView.insertSections([self.messages.count - 1])
            if self.messages.count >= 2 {
                self.messagesCollectionView.reloadSections([self.messages.count - 2])
            }
        }, completion: { [weak self] _ in
            self?.messagesCollectionView.scrollToBottom(animated: true)
            self?.isPerformingBatchUpdates = false
        })
    }

}
Unome
  • 6,750
  • 7
  • 45
  • 87

2 Answers2

1

Use below function, It will works fine.

func insertMessage(_ message: MockMessage) {
    messageList.append(message)
    // Reload last section to update header/footer labels and insert a new one
    messagesCollectionView.performBatchUpdates({
        messagesCollectionView.insertSections([messageList.count - 1])
        if messageList.count >= 2 {
            messagesCollectionView.reloadSections([messageList.count - 2])
        }
    }, completion: { [weak self] _ in
        if self?.isLastSectionVisible() == true {
            self?.messagesCollectionView.scrollToBottom(animated: true)
        }
    })
}
Dhruvin Thumar
  • 190
  • 2
  • 10
  • So you're suggesting that a lastSectionVisible check needs to be made when calling scroll to bottom? – Unome Oct 28 '20 at 19:46
0

Based on new changes from MessageKit you need to use scrollToLastItem() instead of using scrollToBottom(animated:) now:

func insertMessage(_ message: NewMessageModel) {
    DispatchQueue.main.async {
        
        self.isPerformingBatchUpdates = true
        self.messagesCollectionView.performBatchUpdates({
            self.messages.append(message)
            self.checkLastMessageSent(message: message)
            self.messagesCollectionView.insertSections([self.messages.count - 1])
            if self.messages.count >= 2 {
                self.messagesCollectionView.reloadSections([self.messages.count - 2])
            }
        }, completion: { [weak self] _ in
            self?.messagesCollectionView.scrollToLastItem() // Use it instead of using `scrollToBottom(animated:)`
            self?.isPerformingBatchUpdates = false
        })
    }

}