Skip to content

Commit 5c893e7

Browse files
authored
Add logs to detect duplicates in the room list (#5364)
* Add logs to detect duplicates in the room list * Add comments and helper class about the caching results logging
1 parent 0b4b63a commit 5c893e7

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

features/home/impl/src/main/kotlin/io/element/android/features/home/impl/datasource/RoomListDataSource.kt

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import kotlinx.coroutines.flow.onEach
3030
import kotlinx.coroutines.sync.Mutex
3131
import kotlinx.coroutines.sync.withLock
3232
import kotlinx.coroutines.withContext
33+
import timber.log.Timber
3334
import kotlin.time.Duration.Companion.seconds
3435

3536
@Inject
@@ -102,13 +103,44 @@ class RoomListDataSource(
102103
}
103104

104105
private suspend fun buildAndEmitAllRooms(roomSummaries: List<RoomSummary>, useCache: Boolean = true) {
106+
// Used to detect duplicates in the room list summaries - see comment below
107+
data class CacheResult(val index: Int, val fromCache: Boolean)
108+
val cachingResults = mutableMapOf<RoomId, MutableList<CacheResult>>()
109+
105110
val roomListRoomSummaries = diffCache.indices().mapNotNull { index ->
106111
if (useCache) {
107-
diffCache.get(index) ?: buildAndCacheItem(roomSummaries, index)
112+
diffCache.get(index)?.let { cachedItem ->
113+
// Add the cached item to the caching results
114+
val pairs = cachingResults.getOrDefault(cachedItem.roomId, mutableListOf())
115+
pairs.add(CacheResult(index, fromCache = true))
116+
cachingResults[cachedItem.roomId] = pairs
117+
cachedItem
118+
} ?: run {
119+
roomSummaries.getOrNull(index)?.roomId?.let {
120+
// Add the non-cached item to the caching results
121+
val pairs = cachingResults.getOrDefault(it, mutableListOf())
122+
pairs.add(CacheResult(index, fromCache = false))
123+
cachingResults[it] = pairs
124+
}
125+
buildAndCacheItem(roomSummaries, index)
126+
}
108127
} else {
128+
roomSummaries.getOrNull(index)?.roomId?.let {
129+
// Add the non-cached item to the caching results
130+
val pairs = cachingResults.getOrDefault(it, mutableListOf())
131+
pairs.add(CacheResult(index, fromCache = false))
132+
cachingResults[it] = pairs
133+
}
109134
buildAndCacheItem(roomSummaries, index)
110135
}
111136
}
137+
138+
// TODO remove once https://github.com/element-hq/element-x-android/issues/5031 has been confirmed as fixed
139+
val duplicates = cachingResults.filter { (_, operations) -> operations.size > 1 }
140+
if (duplicates.isNotEmpty()) {
141+
Timber.e("Found duplicates in room summaries after an UI update: $duplicates. This could be a race condition/caching issue of some kind")
142+
}
143+
112144
_allRooms.emit(roomListRoomSummaries.toImmutableList())
113145
}
114146

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/roomlist/RoomSummaryListProcessor.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class RoomSummaryListProcessor(
3333
updates.forEach { update ->
3434
applyUpdate(update)
3535
}
36+
37+
// TODO remove once https://github.com/element-hq/element-x-android/issues/5031 has been confirmed as fixed
38+
val duplicates = groupingBy { it.roomId }.eachCount().filter { it.value > 1 }
39+
if (duplicates.isNotEmpty()) {
40+
Timber.e("Found duplicates in room summaries after a list update from the SDK: $duplicates. Updates: $updates")
41+
}
3642
}
3743
}
3844

0 commit comments

Comments
 (0)