Pere Casafont
07/08/2019, 11:25 AMsuspend
modifier to a function that I know it will take long to compute, intellij bugs me with the Redundant 'suspend' modifier
inspection. Am I doing something wrong? All I want is to force this function to be called from a coroutine scope...gildor
07/08/2019, 11:36 AMsuspend
doesn’t mean that it would somehow make blocking code non-blocking, but you can use withContext(<http://Dispatchers.IO/Defatult|Dispatchers.IO/Defatult>) { someBlockingCode }
to move work out of current thread to some IO/Computation dispatchergildor
07/08/2019, 11:37 AMPere Casafont
07/08/2019, 12:26 PMfun calculateExpensiveHash() {
// computations taking 500-1500ms
}
I just want to enforce it being called from a coroutine scope, and to do so I'd add the suspend
modifier...Eric Martori
07/08/2019, 12:34 PMwithContext
which will suspend the calling funtion untill your computation completes:
suspend fun foo() = withContext(<http://Dispatchers.IO|Dispatchers.IO>){
//expensive computation IO computation that returns 1
1
}
Pere Casafont
07/08/2019, 12:37 PMgildor
07/08/2019, 12:50 PMPere Casafont
07/08/2019, 12:53 PMbdawg.io
07/08/2019, 3:45 PMfun CoroutineScope.calculateExpensiveHash() = ...
Pere Casafont
07/08/2019, 4:15 PM