Avoid inconsistent timelines by db insertions before fully loaded chunk

Change-Id: Icaf190630b3af8fec02b77808dd405c9e3598ec5
This commit is contained in:
SpiritCroc 2022-03-21 10:48:55 +01:00
parent 3caee280d5
commit ece413887a

View File

@ -475,6 +475,30 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
private fun handleDatabaseChangeSet(results: RealmResults<TimelineEventEntity>, changeSet: OrderedCollectionChangeSet) {
val insertions = changeSet.insertionRanges
for (range in insertions) {
// Check if the insertion's displayIndices match our expectations - or skip this insertion.
// Inconsistencies (missing messages) can happen otherwise if we get insertions before having loaded all timeline events of the chunk.
if (builtEvents.isNotEmpty()) {
// Check consistency to item before insertions
if (range.startIndex > 0) {
val firstInsertion = results[range.startIndex]!!
val lastBeforeInsertion = builtEvents[range.startIndex-1]
if (firstInsertion.displayIndex+1 != lastBeforeInsertion.displayIndex) {
Timber.i("handleDatabaseChangeSet: skip insertion at ${range.startIndex}/${builtEvents.size}, " +
"displayIndex mismatch at ${range.startIndex}: ${firstInsertion.displayIndex} -> ${lastBeforeInsertion.displayIndex}")
continue
}
}
// Check consistency to item after insertions
if (range.startIndex < builtEvents.size) {
val lastInsertion = results[range.startIndex+range.length-1]!!
val firstAfterInsertion = builtEvents[range.startIndex]
if (firstAfterInsertion.displayIndex+1 != lastInsertion.displayIndex) {
Timber.i("handleDatabaseChangeSet: skip insertion at ${range.startIndex}/${builtEvents.size}, " +
"displayIndex mismatch at ${range.startIndex+range.length}: ${firstAfterInsertion.displayIndex} -> ${lastInsertion.displayIndex}")
continue
}
}
}
val newItems = results
.subList(range.startIndex, range.startIndex + range.length)
.map { it.buildAndDecryptIfNeeded() }