Catch cancellation exceptions for chat room live data

parent 9d9e5d08
...@@ -24,8 +24,6 @@ import kotlinx.coroutines.experimental.* ...@@ -24,8 +24,6 @@ import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.android.UI import kotlinx.coroutines.experimental.android.UI
import timber.log.Timber import timber.log.Timber
import java.lang.IllegalArgumentException import java.lang.IllegalArgumentException
import kotlin.coroutines.experimental.coroutineContext
class ChatRoomsViewModel( class ChatRoomsViewModel(
private val connectionManager: ConnectionManager, private val connectionManager: ConnectionManager,
...@@ -45,11 +43,11 @@ class ChatRoomsViewModel( ...@@ -45,11 +43,11 @@ class ChatRoomsViewModel(
return@switchMap if (query.isSearch()) { return@switchMap if (query.isSearch()) {
this@ChatRoomsViewModel.query.wrap(runContext) { _, data: MutableLiveData<RoomsModel> -> this@ChatRoomsViewModel.query.wrap(runContext) { _, data: MutableLiveData<RoomsModel> ->
try {
val string = (query as Query.Search).query val string = (query as Query.Search).query
// debounce, to not query while the user is writing // debounce, to not query while the user is writing
delay(200) delay(200)
yield()
val rooms = repository.search(string).let { mapper.map(it, showLastMessage = this.showLastMessage) } val rooms = repository.search(string).let { mapper.map(it, showLastMessage = this.showLastMessage) }
data.postValue(rooms.toMutableList() + LoadingItemHolder()) data.postValue(rooms.toMutableList() + LoadingItemHolder())
yield() yield()
...@@ -60,6 +58,9 @@ class ChatRoomsViewModel( ...@@ -60,6 +58,9 @@ class ChatRoomsViewModel(
}.ifNull { }.ifNull {
data.postValue(rooms) data.postValue(rooms)
} }
} catch (ignore: CancellationException) {
}
} }
} else { } else {
repository.getChatRooms(query.asSortingOrder()) repository.getChatRooms(query.asSortingOrder())
......
...@@ -22,20 +22,17 @@ suspend fun <T> retryIO( ...@@ -22,20 +22,17 @@ suspend fun <T> retryIO(
{ {
var currentDelay = initialDelay var currentDelay = initialDelay
repeat(times - 1) { currentTry -> repeat(times - 1) { currentTry ->
if (!coroutineContext.isActive) throw TimeoutCancellationException("job canceled") yield()
try { try {
return block() return block()
} catch (e: RocketChatNetworkErrorException) { } catch (e: RocketChatNetworkErrorException) {
Timber.d(e, "failed call($currentTry): $description") Timber.d(e, "failed call($currentTry): $description")
e.printStackTrace() e.printStackTrace()
} }
if (!coroutineContext.isActive) throw TimeoutCancellationException("job canceled")
delay(currentDelay) delay(currentDelay)
currentDelay = (currentDelay * factor).toLong().coerceAtMost(maxDelay) currentDelay = (currentDelay * factor).toLong().coerceAtMost(maxDelay)
} }
yield()
if (!coroutineContext.isActive) throw TimeoutCancellationException("job canceled")
return block() // last attempt return block() // last attempt
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment