Skip to content

Commit c290d3c

Browse files
Remove exception handling code in UseCase2
1 parent be9411f commit c290d3c

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.cancellation
2+
3+
import kotlinx.coroutines.CancellationException
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.flow.flow
6+
import kotlinx.coroutines.flow.onCompletion
7+
import kotlinx.coroutines.launch
8+
import kotlin.coroutines.EmptyCoroutineContext
9+
10+
suspend fun main() {
11+
val scope = CoroutineScope(EmptyCoroutineContext)
12+
13+
scope.launch {
14+
intFlow()
15+
.onCompletion { throwable ->
16+
if (throwable is CancellationException) {
17+
println("Flow got cancelled.")
18+
}
19+
}
20+
.collect {
21+
println("Collected $it")
22+
}
23+
}.join()
24+
}
25+
26+
private fun intFlow() = flow {
27+
emit(1)
28+
emit(2)
29+
emit(3)
30+
}

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/usecase2/FlowUseCase2ViewModel.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,5 @@ class FlowUseCase2ViewModel(
6969
.onStart {
7070
emit(UiState.Loading)
7171
}
72-
.catch { throwable ->
73-
Timber.tag("Flow").d("Handle exception in catch operator $throwable")
74-
emit(UiState.Error("Something went wrong..."))
75-
}
7672
.asLiveData(defaultDispatcher)
7773
}

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/flow/usecase2/StockPriceDataSource.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import com.lukaslechner.coroutineusecasesonandroid.usecases.flow.mock.Stock
55
import kotlinx.coroutines.delay
66
import kotlinx.coroutines.flow.Flow
77
import kotlinx.coroutines.flow.flow
8-
import kotlinx.coroutines.flow.retry
9-
import retrofit2.HttpException
108
import timber.log.Timber
119

1210
interface StockPriceDataSource {
@@ -22,14 +20,5 @@ class NetworkStockPriceDataSource(mockApi: FlowMockApi) : StockPriceDataSource {
2220
emit(currentStockList)
2321
delay(5_000)
2422
}
25-
}.retry { cause ->
26-
Timber.tag("Flow").d("Enter retry operator with $cause")
27-
val shouldRetry = cause is HttpException
28-
29-
if (shouldRetry) {
30-
delay(5_000)
31-
}
32-
33-
shouldRetry
3423
}
3524
}

0 commit comments

Comments
 (0)