Conversation
|
|
||
| // Those API requests are independent and can be run in parallel | ||
| val storeJob = | ||
| CoroutineScope(coroutineContext).async { | ||
| virtusizeAPIService.getStoreProduct(productId) | ||
| } | ||
| val productTypesJob = | ||
| CoroutineScope(coroutineContext).async { | ||
| virtusizeAPIService.getProductTypes() | ||
| } | ||
| val langJob = | ||
| CoroutineScope(coroutineContext).async { | ||
| virtusizeAPIService.getI18n(language) | ||
| } | ||
|
|
||
| val storeProductResponse = storeJob.await() | ||
| val productTypesResponse = productTypesJob.await() | ||
| val i18nResponse = langJob.await() | ||
|
|
There was a problem hiding this comment.
https://stackoverflow.com/questions/59368838/difference-between-coroutinescope-and-coroutinescope-in-kotlin
For calling async, you can create a coroutine scope as follows:
internal suspend fun fetchInitialData(
language: VirtusizeLanguage?,
product: VirtusizeProduct,
) = coroutineScope {
// optional: It doesn't return a Job instead of Deferred although it inherits from Job.
// It might be better to remove the job naming
val storeProduct = async {
virtusizeAPIService.getStoreProduct(productId)
}
storeProduct.await()
...
}There was a problem hiding this comment.
Often we need to use the result of await() which is technically a desirable object:
val storeProductJob = async {
virtusizeAPIService.getStoreProduct(productId)
}
val storeProduct = storeProductJob.await()Would using another prefix, like storeProductDeferred or storeProductAsync be a better option?
There was a problem hiding this comment.
Coroutine scope created. Still keep the naming though. Let me know if you have a better naming or approach to use result of .await().
There was a problem hiding this comment.
I would think storeProductDeferred or storeProductAsync sounds better, but it's up to you.
virtusize/src/main/java/com/virtusize/android/VirtusizeRepository.kt
Outdated
Show resolved
Hide resolved
virtusize/src/main/java/com/virtusize/android/VirtusizeRepository.kt
Outdated
Show resolved
Hide resolved
virtusize/src/main/java/com/virtusize/android/VirtusizeRepository.kt
Outdated
Show resolved
Hide resolved
virtusize/src/main/java/com/virtusize/android/VirtusizeRepository.kt
Outdated
Show resolved
Hide resolved
As per code review feedback
|
@akueisara fyi, because linter breaks the formatting of the whole |
⬅️ As Is
➡️ To Be
asynccoroutines☑️ Checklist
Log.detc are removedproduction