Merge pull request #5909 from vector-im/defect/adm/crash-when-processing-call-events
Fixes crash when accepting VOIP calls
This commit is contained in:
commit
1d59f69a57
1
changelog.d/5421.bugfix
Normal file
1
changelog.d/5421.bugfix
Normal file
@ -0,0 +1 @@
|
|||||||
|
Fixes crash when accepting or receiving VOIP calls
|
@ -19,6 +19,9 @@ package org.matrix.android.sdk.internal.database
|
|||||||
import com.zhuinden.monarchy.Monarchy
|
import com.zhuinden.monarchy.Monarchy
|
||||||
import io.realm.RealmConfiguration
|
import io.realm.RealmConfiguration
|
||||||
import io.realm.RealmResults
|
import io.realm.RealmResults
|
||||||
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
|
import kotlinx.coroutines.flow.launchIn
|
||||||
|
import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.matrix.android.sdk.internal.database.mapper.asDomain
|
import org.matrix.android.sdk.internal.database.mapper.asDomain
|
||||||
import org.matrix.android.sdk.internal.database.model.EventEntity
|
import org.matrix.android.sdk.internal.database.model.EventEntity
|
||||||
@ -38,10 +41,22 @@ internal class EventInsertLiveObserver @Inject constructor(@SessionDatabase real
|
|||||||
it.where(EventInsertEntity::class.java).equalTo(EventInsertEntityFields.CAN_BE_PROCESSED, true)
|
it.where(EventInsertEntity::class.java).equalTo(EventInsertEntityFields.CAN_BE_PROCESSED, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val onResultsChangedFlow = MutableSharedFlow<RealmResults<EventInsertEntity>>()
|
||||||
|
|
||||||
|
init {
|
||||||
|
onResultsChangedFlow
|
||||||
|
.onEach { handleChange(it) }
|
||||||
|
.launchIn(observerScope)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onChange(results: RealmResults<EventInsertEntity>) {
|
override fun onChange(results: RealmResults<EventInsertEntity>) {
|
||||||
if (!results.isLoaded || results.isEmpty()) {
|
if (!results.isLoaded || results.isEmpty()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
observerScope.launch { onResultsChangedFlow.emit(results) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun handleChange(results: RealmResults<EventInsertEntity>) {
|
||||||
val idsToDeleteAfterProcess = ArrayList<String>()
|
val idsToDeleteAfterProcess = ArrayList<String>()
|
||||||
val filteredEvents = ArrayList<EventInsertEntity>(results.size)
|
val filteredEvents = ArrayList<EventInsertEntity>(results.size)
|
||||||
Timber.v("EventInsertEntity updated with ${results.size} results in db")
|
Timber.v("EventInsertEntity updated with ${results.size} results in db")
|
||||||
@ -58,30 +73,29 @@ internal class EventInsertLiveObserver @Inject constructor(@SessionDatabase real
|
|||||||
}
|
}
|
||||||
idsToDeleteAfterProcess.add(it.eventId)
|
idsToDeleteAfterProcess.add(it.eventId)
|
||||||
}
|
}
|
||||||
observerScope.launch {
|
|
||||||
awaitTransaction(realmConfiguration) { realm ->
|
awaitTransaction(realmConfiguration) { realm ->
|
||||||
Timber.v("##Transaction: There are ${filteredEvents.size} events to process ")
|
Timber.v("##Transaction: There are ${filteredEvents.size} events to process ")
|
||||||
filteredEvents.forEach { eventInsert ->
|
filteredEvents.forEach { eventInsert ->
|
||||||
val eventId = eventInsert.eventId
|
val eventId = eventInsert.eventId
|
||||||
val event = EventEntity.where(realm, eventId).findFirst()
|
val event = EventEntity.where(realm, eventId).findFirst()
|
||||||
if (event == null) {
|
if (event == null) {
|
||||||
Timber.v("Event $eventId not found")
|
Timber.v("Event $eventId not found")
|
||||||
return@forEach
|
return@forEach
|
||||||
}
|
}
|
||||||
val domainEvent = event.asDomain()
|
val domainEvent = event.asDomain()
|
||||||
processors.filter {
|
processors.filter {
|
||||||
it.shouldProcess(eventId, domainEvent.getClearType(), eventInsert.insertType)
|
it.shouldProcess(eventId, domainEvent.getClearType(), eventInsert.insertType)
|
||||||
}.forEach {
|
}.forEach {
|
||||||
it.process(realm, domainEvent)
|
it.process(realm, domainEvent)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
realm.where(EventInsertEntity::class.java)
|
|
||||||
.`in`(EventInsertEntityFields.EVENT_ID, idsToDeleteAfterProcess.toTypedArray())
|
|
||||||
.findAll()
|
|
||||||
.deleteAllFromRealm()
|
|
||||||
}
|
}
|
||||||
processors.forEach { it.onPostProcess() }
|
realm.where(EventInsertEntity::class.java)
|
||||||
|
.`in`(EventInsertEntityFields.EVENT_ID, idsToDeleteAfterProcess.toTypedArray())
|
||||||
|
.findAll()
|
||||||
|
.deleteAllFromRealm()
|
||||||
}
|
}
|
||||||
|
processors.forEach { it.onPostProcess() }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun shouldProcess(eventInsertEntity: EventInsertEntity): Boolean {
|
private fun shouldProcess(eventInsertEntity: EventInsertEntity): Boolean {
|
||||||
|
Loading…
Reference in New Issue
Block a user