Skip to content

Commit

Permalink
Merge pull request #6474 from vector-im/feature/fga/fix_6463
Browse files Browse the repository at this point in the history
Fix crashes when opening Thread (#6463)
  • Loading branch information
ouchadam authored Jul 7, 2022
2 parents 4126e64 + 19fc97b commit 3e770f9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions changelog.d/6463.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crashes when opening Thread
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import io.realm.Realm
import io.realm.RealmConfiguration
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.android.asCoroutineDispatcher
import kotlinx.coroutines.cancelChildren
Expand Down Expand Up @@ -116,6 +117,7 @@ internal class DefaultTimeline(
)

private var strategy: LoadTimelineStrategy = buildStrategy(LoadTimelineStrategy.Mode.Live)
private var startTimelineJob: Job? = null

override val isLive: Boolean
get() = !getPaginationState(Timeline.Direction.FORWARDS).hasMoreToLoad
Expand Down Expand Up @@ -143,7 +145,7 @@ internal class DefaultTimeline(
timelineScope.launch {
loadRoomMembersIfNeeded()
}
timelineScope.launch {
startTimelineJob = timelineScope.launch {
sequencer.post {
if (isStarted.compareAndSet(false, true)) {
isFromThreadTimeline = rootThreadEventId != null
Expand Down Expand Up @@ -174,8 +176,10 @@ internal class DefaultTimeline(

override fun restartWithEventId(eventId: String?) {
timelineScope.launch {
openAround(eventId, rootThreadEventId)
postSnapshot()
sequencer.post {
openAround(eventId, rootThreadEventId)
postSnapshot()
}
}
}

Expand All @@ -185,6 +189,7 @@ internal class DefaultTimeline(

override fun paginate(direction: Timeline.Direction, count: Int) {
timelineScope.launch {
startTimelineJob?.join()
val postSnapshot = loadMore(count, direction, fetchOnServerIfNeeded = true)
if (postSnapshot) {
postSnapshot()
Expand All @@ -193,6 +198,7 @@ internal class DefaultTimeline(
}

override suspend fun awaitPaginate(direction: Timeline.Direction, count: Int): List<TimelineEvent> {
startTimelineJob?.join()
withContext(timelineDispatcher) {
loadMore(count, direction, fetchOnServerIfNeeded = true)
}
Expand Down Expand Up @@ -279,6 +285,7 @@ internal class DefaultTimeline(
direction = Timeline.Direction.BACKWARDS,
fetchOnServerIfNeeded = false
)

Timber.v("$baseLogMessage finished")
}

Expand Down Expand Up @@ -312,9 +319,11 @@ internal class DefaultTimeline(

private fun onLimitedTimeline() {
timelineScope.launch {
initPaginationStates(null)
loadMore(settings.initialSize, Timeline.Direction.BACKWARDS, false)
postSnapshot()
sequencer.post {
initPaginationStates(null)
loadMore(settings.initialSize, Timeline.Direction.BACKWARDS, false)
postSnapshot()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import io.realm.Realm
import io.realm.RealmConfiguration
import io.realm.RealmResults
import io.realm.kotlin.createObject
import io.realm.kotlin.executeTransactionAwait
import kotlinx.coroutines.CompletableDeferred
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
import org.matrix.android.sdk.api.extensions.orFalse
Expand Down Expand Up @@ -265,7 +266,7 @@ internal class LoadTimelineStrategy constructor(
}
}

private fun getChunkEntity(realm: Realm): RealmResults<ChunkEntity> {
private suspend fun getChunkEntity(realm: Realm): RealmResults<ChunkEntity> {
return when (mode) {
is Mode.Live -> {
ChunkEntity.where(realm, roomId)
Expand All @@ -289,8 +290,8 @@ internal class LoadTimelineStrategy constructor(
* Clear any existing thread chunk entity and create a new one, with the
* rootThreadEventId included.
*/
private fun recreateThreadChunkEntity(realm: Realm, rootThreadEventId: String) {
realm.executeTransaction {
private suspend fun recreateThreadChunkEntity(realm: Realm, rootThreadEventId: String) {
realm.executeTransactionAwait {
// Lets delete the chunk and start a new one
ChunkEntity.findLastForwardChunkOfThread(it, roomId, rootThreadEventId)?.deleteAndClearThreadEvents()?.let {
Timber.i("###THREADS LoadTimelineStrategy [onStart] thread chunk cleared..")
Expand All @@ -309,8 +310,8 @@ internal class LoadTimelineStrategy constructor(
/**
* Clear any existing thread chunk.
*/
private fun clearThreadChunkEntity(realm: Realm, rootThreadEventId: String) {
realm.executeTransaction {
private suspend fun clearThreadChunkEntity(realm: Realm, rootThreadEventId: String) {
realm.executeTransactionAwait {
ChunkEntity.findLastForwardChunkOfThread(it, roomId, rootThreadEventId)?.deleteAndClearThreadEvents()?.let {
Timber.i("###THREADS LoadTimelineStrategy [onStop] thread chunk cleared..")
}
Expand Down

0 comments on commit 3e770f9

Please sign in to comment.