New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ASK] How to create sequential or parallel request? #11
Comments
Hi, you can check merge. |
If i look at documentation merge can only be use with ApiResponse<List>, but my requirement is not list. So i cant use that |
I got it. val response1 = withContext(Dispatchers.IO) { pokedexClient.fetchPokemonInfo(name = name1) }
val flow1: Flow<PokemonInfo> = response1.toFlow()
val response2 = withContext(Dispatchers.IO) { pokedexClient.fetchPokemonInfo(name = name2) }
val flow2: Flow<PokemonInfo> = response2.toFlow()
val mergedFlow: Flow<String> = flow1.combine(flow2) { info1, info2 ->
"${info1.name}, ${info2.name}"
}
mergedFlow.collect {
Log.e("Test", "merged information: $it")
} |
And you can handle the error case like this: val response1 = withContext(Dispatchers.IO) { pokedexClient.fetchPokemonInfo(name = "bulbasaur") }
val flow1: Flow<PokemonInfo> = response1.toFlow()
val response2 = withContext(Dispatchers.IO) {
pokedexClient.fetchPokemonInfo(name = "Wrong name").onError {
Log.e("Test", "You've got an error by fetching response2..") }
}
val flow2: Flow<PokemonInfo> = response2.toFlow()
val mergedFlow: Flow<String> = flow1.combine(flow2) { info1, info2 ->
"${info1.name}, ${info2.name}"
}
mergedFlow.collect {
Log.e("Test", "merged information: $it")
} |
And here is an example that you can just request sequentially and emit data as a single Flow. flow {
pokedexClient.fetchPokemonInfo(name = "bulbasaur")
.suspendOnSuccess { emit(data?.name!! + " response1") }
.onError {
Log.e("Test", "You've got an error by fetching response1..")
}
pokedexClient.fetchPokemonInfo(name = "bulbasaur")
.suspendOnSuccess { emit(data?.name!! + " response2") }
.onError {
Log.e("Test", "You've got an error by fetching response2..")
}
}.flowOn(Dispatchers.IO).collect {
Log.e("Test", "collected response: $it")
} |
thankyou i will explore it 👍 |
@skydoves
How can I achieve it? it must be called one by one in order. |
right now i'm using
CoroutinesResponseCallAdapterFactory
, how to create sequential request and parallel request with sandwich?Example for sequential = i have API for getConfig and getBanner, frist getConfig, when succeed it will call getBanner
Example for parallel = API getProductDetail, getUserLimit. both API will parralel then result of 2 api will combine into 1 result
thank you
The text was updated successfully, but these errors were encountered: