Realm: remove usage of freeze as it was not necessary (unique thread)

This commit is contained in:
ganfra 2022-02-22 12:45:10 +01:00
parent 116f7d0575
commit fcca75ee23
1 changed files with 10 additions and 11 deletions

View File

@ -90,8 +90,7 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
private val timelineEventsChangeListener = private val timelineEventsChangeListener =
OrderedRealmCollectionChangeListener { results: RealmResults<TimelineEventEntity>, changeSet: OrderedCollectionChangeSet -> OrderedRealmCollectionChangeListener { results: RealmResults<TimelineEventEntity>, changeSet: OrderedCollectionChangeSet ->
Timber.v("on timeline events chunk update") Timber.v("on timeline events chunk update")
val frozenResults = results.freeze() handleDatabaseChangeSet(results, changeSet)
handleDatabaseChangeSet(frozenResults, changeSet)
} }
private var timelineEventEntities: RealmResults<TimelineEventEntity> = chunkEntity.sortedTimelineEvents(timelineSettings.rootThreadEventId) private var timelineEventEntities: RealmResults<TimelineEventEntity> = chunkEntity.sortedTimelineEvents(timelineSettings.rootThreadEventId)
@ -287,7 +286,7 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
* @return the number of events loaded. If we are in a thread timeline it also returns * @return the number of events loaded. If we are in a thread timeline it also returns
* whether or not we reached the end/root message * whether or not we reached the end/root message
*/ */
private suspend fun loadFromStorage(count: Int, direction: Timeline.Direction): LoadedFromStorage { private fun loadFromStorage(count: Int, direction: Timeline.Direction): LoadedFromStorage {
val displayIndex = getNextDisplayIndex(direction) ?: return LoadedFromStorage() val displayIndex = getNextDisplayIndex(direction) ?: return LoadedFromStorage()
val baseQuery = timelineEventEntities.where() val baseQuery = timelineEventEntities.where()
@ -428,10 +427,10 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
* This method is responsible for managing insertions and updates of events on this chunk. * This method is responsible for managing insertions and updates of events on this chunk.
* *
*/ */
private fun handleDatabaseChangeSet(frozenResults: RealmResults<TimelineEventEntity>, changeSet: OrderedCollectionChangeSet) { private fun handleDatabaseChangeSet(results: RealmResults<TimelineEventEntity>, changeSet: OrderedCollectionChangeSet) {
val insertions = changeSet.insertionRanges val insertions = changeSet.insertionRanges
for (range in insertions) { for (range in insertions) {
val newItems = frozenResults val newItems = results
.subList(range.startIndex, range.startIndex + range.length) .subList(range.startIndex, range.startIndex + range.length)
.map { it.buildAndDecryptIfNeeded() } .map { it.buildAndDecryptIfNeeded() }
builtEventsIndexes.entries.filter { it.value >= range.startIndex }.forEach { it.setValue(it.value + range.length) } builtEventsIndexes.entries.filter { it.value >= range.startIndex }.forEach { it.setValue(it.value + range.length) }
@ -447,7 +446,7 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
val modifications = changeSet.changeRanges val modifications = changeSet.changeRanges
for (range in modifications) { for (range in modifications) {
for (modificationIndex in (range.startIndex until range.startIndex + range.length)) { for (modificationIndex in (range.startIndex until range.startIndex + range.length)) {
val updatedEntity = frozenResults[modificationIndex] ?: continue val updatedEntity = results[modificationIndex] ?: continue
try { try {
builtEvents[modificationIndex] = updatedEntity.buildAndDecryptIfNeeded() builtEvents[modificationIndex] = updatedEntity.buildAndDecryptIfNeeded()
} catch (failure: Throwable) { } catch (failure: Throwable) {
@ -458,20 +457,20 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity,
if (insertions.isNotEmpty() || modifications.isNotEmpty()) { if (insertions.isNotEmpty() || modifications.isNotEmpty()) {
onBuiltEvents(true) onBuiltEvents(true)
} }
} }
private fun getNextDisplayIndex(direction: Timeline.Direction): Int? { private fun getNextDisplayIndex(direction: Timeline.Direction): Int? {
val frozenTimelineEvents = timelineEventEntities.freeze() if (timelineEventEntities.isEmpty()) {
if (frozenTimelineEvents.isEmpty()) {
return null return null
} }
return if (builtEvents.isEmpty()) { return if (builtEvents.isEmpty()) {
if (initialEventId != null) { if (initialEventId != null) {
frozenTimelineEvents.where().equalTo(TimelineEventEntityFields.EVENT_ID, initialEventId).findFirst()?.displayIndex timelineEventEntities.where().equalTo(TimelineEventEntityFields.EVENT_ID, initialEventId).findFirst()?.displayIndex
} else if (direction == Timeline.Direction.BACKWARDS) { } else if (direction == Timeline.Direction.BACKWARDS) {
frozenTimelineEvents.first(null)?.displayIndex timelineEventEntities.first(null)?.displayIndex
} else { } else {
frozenTimelineEvents.last(null)?.displayIndex timelineEventEntities.last(null)?.displayIndex
} }
} else if (direction == Timeline.Direction.FORWARDS) { } else if (direction == Timeline.Direction.FORWARDS) {
builtEvents.first().displayIndex + 1 builtEvents.first().displayIndex + 1