Hi, I have a problem that i would like to solve using coroutines but haven’t found a successful way to do it. I have a while loop, that reads input from an input stream (bluetooth socket on android) until a specific number of words have been received. However, the while loop could run forever incase the specific number of words has not been sent (and the socket connection is still active). To mitigate this, i would like to have two operations running, one that runs a timeout (delay) and the other to do the actual work. These operations would run concurrently and i would be interested in getting the completion of the fastest operation. This way i can be sure that i would not wait for a result forever and i can handle the exception.
One approach i tried was using withTimeout, but it would never really timeout (not sure why)
withTimeout(20000) {
try{
while (true) {
// read input from socket and exit while block when exception is thrown
// or expected data has been received
}
}catch(e: Exception) {
// Handle the exception here
}
}
I also tried to use this, but the code within`scope.launch` was never executed but the timeout worked as expected
val scope: CoroutineScope = CoroutineScope(Job() + <http://Dispatchers.IO|Dispatchers.IO>)
withTimeout(20000){
suspendCancellableCoroutine<String>{ continuation ->
continuation.invokeOnCancellation {
// Handle my exception here
}
scope.launch {
while (true) {
// read input from socket and exit while block when exception is thrown
// or expected data has been received
}
}
}
}
Could someone please help give me some pointers in what i might be missing or even perhaps a better way to approach this?