Skip to content

Commit b386b87

Browse files
Fix for concurrency bug (alamkanak#160)
* fix for concurrency bug * minimized diff * used CopyOnWriteArrayList Co-authored-by: Felix Felten <[email protected]>
1 parent 182641f commit b386b87

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

core/src/main/java/com/alamkanak/weekview/EventChipsCache.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package com.alamkanak.weekview
22

33
import java.util.Calendar
44
import java.util.concurrent.ConcurrentHashMap
5+
import java.util.concurrent.CopyOnWriteArrayList
56

67
internal class EventChipsCache {
78

89
val allEventChips: List<EventChip>
910
get() = normalEventChipsByDate.values.flatten() + allDayEventChipsByDate.values.flatten()
1011

11-
private val normalEventChipsByDate = ConcurrentHashMap<Long, MutableList<EventChip>>()
12-
private val allDayEventChipsByDate = ConcurrentHashMap<Long, MutableList<EventChip>>()
12+
private val normalEventChipsByDate = ConcurrentHashMap<Long, CopyOnWriteArrayList<EventChip>>()
13+
private val allDayEventChipsByDate = ConcurrentHashMap<Long, CopyOnWriteArrayList<EventChip>>()
1314

1415
fun allEventChipsInDateRange(
1516
dateRange: List<Calendar>
@@ -62,11 +63,11 @@ internal class EventChipsCache {
6263
normalEventChipsByDate.clear()
6364
}
6465

65-
private fun ConcurrentHashMap<Long, MutableList<EventChip>>.addOrReplace(
66+
private fun ConcurrentHashMap<Long, CopyOnWriteArrayList<EventChip>>.addOrReplace(
6667
key: Long,
6768
eventChip: EventChip
6869
) {
69-
val results = getOrElse(key) { mutableListOf() }
70+
val results = getOrElse(key) { CopyOnWriteArrayList() }
7071
val indexOfExisting = results.indexOfFirst { it.event.id == eventChip.event.id }
7172
if (indexOfExisting != -1) {
7273
// If an event with the same ID already exists, replace it. The new event will likely be
@@ -76,7 +77,6 @@ internal class EventChipsCache {
7677
} else {
7778
results.add(eventChip)
7879
}
79-
8080
this[key] = results
8181
}
8282
}

0 commit comments

Comments
 (0)