Joe Altidore
08/20/2022, 10:23 PMsuspend fun isFinished(token: String): Boolean {
return coroutineScope {
val a = async {
updateProfile(token = token)
}
val b = async{
updateStatus(token = token)
}
(a.await()) && (b.await())
}
}
Methods updateProfile(token: String) and updateStatus(token: String) are the api methodsJoffrey
08/20/2022, 10:30 PMawait
cancels the other async
, it probably means the first call failed with an exception. You should check that.Joe Altidore
08/21/2022, 5:38 AMJoffrey
08/21/2022, 7:31 AMb.await()
fails when you swap? Is that not an exception that is thrown?runBlocking
, it could be that both calls are using something that is not thread safe. Did you check that? It would be nice if you could share enough code to reproduce the problemuli
08/21/2022, 11:06 AMJoe Altidore
08/21/2022, 1:20 PMsuspend fun isFinished(token: String): Boolean {
val a = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).async{
updateProfile(token = token)
}
val b = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).async{
updateStatus(token = token)
}
val result = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).async {
a.await() && b.await()
}
return result.await()
}
But I feel there could be a better wayuli
08/21/2022, 1:27 PMJoffrey
08/21/2022, 8:47 PMlouiscad
08/23/2022, 6:21 PMupdateXxx
functions with delay(…)
calls instead, you'll see it works perfectly with your original snippet.Joe Altidore
08/23/2022, 7:17 PMupdateProfile()
and updateStatus()
methods are recursive methods and I am suspecting that to be the reason.louiscad
08/23/2022, 7:23 PMsuspend fun isFinished(token: String): Boolean {
return coroutineScope {
val a = async {
delay(3.seconds)
true
}
val b = async{
delay(2.seconds)
true
}
a.await() && b.await()
}
}
You'll see that it finishes successfully.await()
calls, the parenthses around the calls are useless. I edited my snippet to reflect that.Joe Altidore
08/23/2022, 7:28 PMlouiscad
08/23/2022, 7:30 PMJoe Altidore
08/23/2022, 7:31 PMJoffrey
08/23/2022, 7:32 PMJoe Altidore
08/23/2022, 7:36 PMprivate suspend fun getExpense(offset: Int, token: String): Response{
val expense = api.syncExpense(offset, token)
return if(expense is Resource.Success){
val res = expense.data as ExpenseDto.Data
if(res.rows.isNotEmpty()){
expenseDao.addExpenses(res.rows.map { it.toExpenseEntity() })
if(res.rows.size == LIMIT){
return getExpense(offset + LIMIT, token)
}
}
res
}else{
(expense as Resource.Failure).data!!
}
}
uli
08/23/2022, 7:41 PMlouiscad
08/23/2022, 8:01 PMJoe Altidore
08/23/2022, 8:20 PMlouiscad
08/23/2022, 8:49 PMsuspend fun isFinished(token: String): Boolean
)Joe Altidore
08/23/2022, 8:53 PM