RoomList : avoid using flow extension on realm results (leads to frozen object and leaks).

This commit is contained in:
ganfra 2022-03-24 15:41:42 +01:00
parent 70e5698082
commit 745382cdfa
6 changed files with 19 additions and 25 deletions

View file

@ -218,9 +218,9 @@ interface RoomService {
sortOrder: RoomSortOrder = RoomSortOrder.ACTIVITY): UpdatableLivePageResult sortOrder: RoomSortOrder = RoomSortOrder.ACTIVITY): UpdatableLivePageResult
/** /**
* Retrieve a flow on the number of rooms. * Retrieve a LiveData on the number of rooms.
*/ */
fun getRoomCountFlow(queryParams: RoomSummaryQueryParams): Flow<Int> fun getRoomCountLive(queryParams: RoomSummaryQueryParams): LiveData<Int>
/** /**
* TODO Doc * TODO Doc

View file

@ -110,8 +110,8 @@ internal class DefaultRoomService @Inject constructor(
return roomSummaryDataSource.getUpdatablePagedRoomSummariesLive(queryParams, pagedListConfig, sortOrder) return roomSummaryDataSource.getUpdatablePagedRoomSummariesLive(queryParams, pagedListConfig, sortOrder)
} }
override fun getRoomCountFlow(queryParams: RoomSummaryQueryParams): Flow<Int> { override fun getRoomCountLive(queryParams: RoomSummaryQueryParams): LiveData<Int> {
return roomSummaryDataSource.getCountFlow(queryParams) return roomSummaryDataSource.getCountLive(queryParams)
} }
override fun getNotificationCountForRooms(queryParams: RoomSummaryQueryParams): RoomAggregateNotificationCount { override fun getNotificationCountForRooms(queryParams: RoomSummaryQueryParams): RoomAggregateNotificationCount {

View file

@ -25,12 +25,7 @@ import androidx.paging.PagedList
import com.zhuinden.monarchy.Monarchy import com.zhuinden.monarchy.Monarchy
import io.realm.Realm import io.realm.Realm
import io.realm.RealmQuery import io.realm.RealmQuery
import io.realm.kotlin.toFlow
import io.realm.kotlin.where import io.realm.kotlin.where
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
import org.matrix.android.sdk.api.query.ActiveSpaceFilter import org.matrix.android.sdk.api.query.ActiveSpaceFilter
import org.matrix.android.sdk.api.query.RoomCategoryFilter import org.matrix.android.sdk.api.query.RoomCategoryFilter
@ -241,15 +236,14 @@ internal class RoomSummaryDataSource @Inject constructor(
} }
} }
fun getCountFlow(queryParams: RoomSummaryQueryParams): Flow<Int> = fun getCountLive(queryParams: RoomSummaryQueryParams): LiveData<Int> {
realmSessionProvider val liveRooms = monarchy.findAllManagedWithChanges {
.withRealm { realm -> roomSummariesQuery(realm, queryParams).findAllAsync() } roomSummariesQuery(it, queryParams)
.toFlow() }
// need to create the flow on a context dispatcher with a thread with attached Looper return Transformations.map(liveRooms) {
.flowOn(coroutineDispatchers.main) it.realmResults.count()
.map { it.size } }
.flowOn(coroutineDispatchers.io) }
.distinctUntilChanged()
fun getNotificationCountForRooms(queryParams: RoomSummaryQueryParams): RoomAggregateNotificationCount { fun getNotificationCountForRooms(queryParams: RoomSummaryQueryParams): RoomAggregateNotificationCount {
var notificationCount: RoomAggregateNotificationCount? = null var notificationCount: RoomAggregateNotificationCount? = null

View file

@ -75,7 +75,7 @@ class RoomListSectionBuilderGroup(
onUpdatable(updatableFilterLivePageResult) onUpdatable(updatableFilterLivePageResult)
val itemCountFlow = updatableFilterLivePageResult.livePagedList.asFlow() val itemCountFlow = updatableFilterLivePageResult.livePagedList.asFlow()
.flatMapLatest { session.getRoomCountFlow(updatableFilterLivePageResult.queryParams) } .flatMapLatest { session.getRoomCountLive(updatableFilterLivePageResult.queryParams).asFlow() }
.distinctUntilChanged() .distinctUntilChanged()
sections.add( sections.add(
@ -276,7 +276,7 @@ class RoomListSectionBuilderGroup(
sectionName = name, sectionName = name,
livePages = livePagedList, livePages = livePagedList,
notifyOfLocalEcho = notifyOfLocalEcho, notifyOfLocalEcho = notifyOfLocalEcho,
itemCount = session.getRoomCountFlow(roomQueryParams) itemCount = session.getRoomCountLive(roomQueryParams).asFlow()
) )
) )
} }

View file

@ -94,7 +94,7 @@ class RoomListSectionBuilderSpace(
onUpdatable(updatableFilterLivePageResult) onUpdatable(updatableFilterLivePageResult)
val itemCountFlow = updatableFilterLivePageResult.livePagedList.asFlow() val itemCountFlow = updatableFilterLivePageResult.livePagedList.asFlow()
.flatMapLatest { session.getRoomCountFlow(updatableFilterLivePageResult.queryParams) } .flatMapLatest { session.getRoomCountLive(updatableFilterLivePageResult.queryParams).asFlow() }
.distinctUntilChanged() .distinctUntilChanged()
sections.add( sections.add(
@ -400,7 +400,7 @@ class RoomListSectionBuilderSpace(
val itemCountFlow = livePagedList.asFlow() val itemCountFlow = livePagedList.asFlow()
.flatMapLatest { .flatMapLatest {
val queryParams = roomQueryParams.process(spaceFilterStrategy, appStateHandler.safeActiveSpaceId()) val queryParams = roomQueryParams.process(spaceFilterStrategy, appStateHandler.safeActiveSpaceId())
session.getRoomCountFlow(queryParams) session.getRoomCountLive(queryParams).asFlow()
} }
.distinctUntilChanged() .distinctUntilChanged()

View file

@ -84,7 +84,7 @@ class SpaceAddRoomsViewModel @AssistedInject constructor(
val spaceCountFlow: Flow<Int> by lazy { val spaceCountFlow: Flow<Int> by lazy {
spaceUpdatableLivePageResult.livePagedList.asFlow() spaceUpdatableLivePageResult.livePagedList.asFlow()
.flatMapLatest { session.getRoomCountFlow(spaceUpdatableLivePageResult.queryParams) } .flatMapLatest { session.getRoomCountLive(spaceUpdatableLivePageResult.queryParams).asFlow() }
.distinctUntilChanged() .distinctUntilChanged()
} }
@ -110,7 +110,7 @@ class SpaceAddRoomsViewModel @AssistedInject constructor(
val roomCountFlow: Flow<Int> by lazy { val roomCountFlow: Flow<Int> by lazy {
roomUpdatableLivePageResult.livePagedList.asFlow() roomUpdatableLivePageResult.livePagedList.asFlow()
.flatMapLatest { session.getRoomCountFlow(roomUpdatableLivePageResult.queryParams) } .flatMapLatest { session.getRoomCountLive(roomUpdatableLivePageResult.queryParams).asFlow() }
.distinctUntilChanged() .distinctUntilChanged()
} }
@ -136,7 +136,7 @@ class SpaceAddRoomsViewModel @AssistedInject constructor(
val dmCountFlow: Flow<Int> by lazy { val dmCountFlow: Flow<Int> by lazy {
dmUpdatableLivePageResult.livePagedList.asFlow() dmUpdatableLivePageResult.livePagedList.asFlow()
.flatMapLatest { session.getRoomCountFlow(dmUpdatableLivePageResult.queryParams) } .flatMapLatest { session.getRoomCountLive(dmUpdatableLivePageResult.queryParams).asFlow() }
.distinctUntilChanged() .distinctUntilChanged()
} }