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 🐛: Bugfix 🐛:
- Do not show long click help if only invitation are displayed - Do not show long click help if only invitation are displayed
- Fix emoji filtering not working - Fix emoji filtering not working
- Fix issue of closing Realm in another thread (#725)
Translations 🗣: Translations 🗣:
- -

View File

@ -30,25 +30,26 @@ internal class DefaultFilterRepository @Inject constructor(private val monarchy:
override suspend fun storeFilter(filterBody: FilterBody, roomEventFilter: RoomEventFilter): Boolean { override suspend fun storeFilter(filterBody: FilterBody, roomEventFilter: RoomEventFilter): Boolean {
return Realm.getInstance(monarchy.realmConfiguration).use { realm -> return Realm.getInstance(monarchy.realmConfiguration).use { realm ->
val filter = FilterEntity.getFilter(realm) val filter = FilterEntity.getFilter(realm)
val result = if (filter.filterBodyJson != filterBody.toJSONString()) { // Filter has changed, or no filter Id yet
// Filter has changed, store it and reset the filter Id filter.filterBodyJson != filterBody.toJSONString()
monarchy.awaitTransaction { || 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 // We manage only one filter for now
val filterBodyJson = filterBody.toJSONString() val filterBodyJson = filterBody.toJSONString()
val roomEventFilterJson = roomEventFilter.toJSONString() val roomEventFilterJson = roomEventFilter.toJSONString()
val filterEntity = FilterEntity.getFilter(it) val filterEntity = FilterEntity.getFilter(realm)
filterEntity.filterBodyJson = filterBodyJson filterEntity.filterBodyJson = filterBodyJson
filterEntity.roomEventFilterJson = roomEventFilterJson filterEntity.roomEventFilterJson = roomEventFilterJson
// Reset filterId // Reset filterId
filterEntity.filterId = "" filterEntity.filterId = ""
} }
true
} else {
filter.filterId.isBlank()
} }
result
} }
} }