Fix issue of closing Realm in another thread (#725)

This commit is contained in:
Benoit Marty 2019-12-03 10:05:10 +01:00
parent 5b63856d96
commit 490ce4b51d
2 changed files with 10 additions and 8 deletions

View File

@ -14,6 +14,7 @@ Other changes:
Bugfix 🐛:
- Do not show long click help if only invitation are displayed
- Fix emoji filtering not working
- Fix issue of closing Realm in another thread (#725)
Translations 🗣:
-

View File

@ -30,25 +30,26 @@ internal class DefaultFilterRepository @Inject constructor(private val monarchy:
override suspend fun storeFilter(filterBody: FilterBody, roomEventFilter: RoomEventFilter): Boolean {
return Realm.getInstance(monarchy.realmConfiguration).use { realm ->
val filter = FilterEntity.getFilter(realm)
val result = if (filter.filterBodyJson != filterBody.toJSONString()) {
// Filter has changed, store it and reset the filter Id
monarchy.awaitTransaction {
// Filter has changed, or no filter Id yet
filter.filterBodyJson != filterBody.toJSONString()
|| filter.filterId.isBlank()
}.also { hasChanged ->
if (hasChanged) {
// Filter is new or has changed, store it and reset the filter Id.
// This has to be done outside of the Realm.use(), because awaitTransaction change the current thread
monarchy.awaitTransaction { realm ->
// We manage only one filter for now
val filterBodyJson = filterBody.toJSONString()
val roomEventFilterJson = roomEventFilter.toJSONString()
val filterEntity = FilterEntity.getFilter(it)
val filterEntity = FilterEntity.getFilter(realm)
filterEntity.filterBodyJson = filterBodyJson
filterEntity.roomEventFilterJson = roomEventFilterJson
// Reset filterId
filterEntity.filterId = ""
}
true
} else {
filter.filterId.isBlank()
}
result
}
}