-
Notifications
You must be signed in to change notification settings - Fork 411
/
PerformNetworkRequestsConcurrentlyViewModel.kt
79 lines (63 loc) 路 3.21 KB
/
PerformNetworkRequestsConcurrentlyViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase3
import androidx.lifecycle.viewModelScope
import com.lukaslechner.coroutineusecasesonandroid.base.BaseViewModel
import com.lukaslechner.coroutineusecasesonandroid.mock.MockApi
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch
class PerformNetworkRequestsConcurrentlyViewModel(
private val mockApi: MockApi = mockApi()
) : BaseViewModel<UiState>() {
fun performNetworkRequestsSequentially() {
uiState.value = UiState.Loading
viewModelScope.launch {
try {
val oreoFeatures = mockApi.getAndroidVersionFeatures(27)
val pieFeatures = mockApi.getAndroidVersionFeatures(28)
val android10Features = mockApi.getAndroidVersionFeatures(29)
val versionFeatures = listOf(oreoFeatures, pieFeatures, android10Features)
uiState.value = UiState.Success(versionFeatures)
} catch (exception: Exception) {
uiState.value = UiState.Error("Network Request failed")
}
}
}
fun performNetworkRequestsConcurrently() {
uiState.value = UiState.Loading
val oreoFeaturesDeferred = viewModelScope.async { mockApi.getAndroidVersionFeatures(27) }
val pieFeaturesDeferred = viewModelScope.async { mockApi.getAndroidVersionFeatures(28) }
val android10FeaturesDeferred =
viewModelScope.async { mockApi.getAndroidVersionFeatures(29) }
viewModelScope.launch {
try {
val versionFeatures =
awaitAll(oreoFeaturesDeferred, pieFeaturesDeferred, android10FeaturesDeferred)
uiState.value = UiState.Success(versionFeatures)
} catch (exception: Exception) {
uiState.value = UiState.Error("Network Request failed")
}
}
/*
Alternatively:
viewModelScope.launch {
try {
// we need to wrap this code with a coroutineScope block
// otherwise the app would crash on unsuccessful network requests
coroutineScope {
val oreoFeaturesDeferred = async { mockApi.getAndroidVersionFeatures(27) }
val pieFeaturesDeferred = async { mockApi.getAndroidVersionFeatures(28) }
val android10FeaturesDeferred = async { mockApi.getAndroidVersionFeatures(29) }
val oreoFeatures = oreoFeaturesDeferred.await()
val pieFeatures = pieFeaturesDeferred.await()
val android10Features = android10FeaturesDeferred.await()
val versionFeatures = listOf(oreoFeatures, pieFeatures, android10Features)
// other alternative: (but slightly different behavior when a deferred fails, see docs)
// val versionFeatures = awaitAll(oreoFeaturesDeferred, pieFeaturesDeferred, android10FeaturesDeferred)
uiState.value = UiState.Success(versionFeatures)
}
} catch (exception: Exception) {
uiState.value = UiState.Error("Network Request failed")
}
}*/
}
}