https://kotlinlang.org logo
#coroutines
Title
# coroutines
h

Hexa

05/13/2019, 1:05 PM
if I have a coroutine like this:
Copy code
val  deleteJob = runBlocking {
                    withTimeoutOrNull(10000L) {
                        launch(<http://Dispatchers.IO|Dispatchers.IO>) { s.deleteItem(xyz) }
                        launch(<http://Dispatchers.IO|Dispatchers.IO>) { s.deleteItem(abc) }
                    }
                }
which one of these is the best way to handle a couroutine that timedout?
Copy code
if(deleteJob!!.isCancelled){
                    //do something
                }
// or
Copy code
if(deleteJob == null){
                    //do something
                }
r

r2vq

05/13/2019, 1:14 PM
Is this a Coroutine specific question or just a handling of
Booleans?
question?
Copy code
if (deleteJob.isCancelled != false) {
    // do something
}
That would handle both cases without risk of NPE right?
d

dector

05/13/2019, 1:18 PM
I suppose that
deleteJob
will be always job for
s.deleteItem(abc)
or
null
.
h

Hexa

05/13/2019, 7:27 PM
thanks @r2vq, and @dector , just to clarify, will
if (deleteJob.isCancelled != false)
evaluate to true if the if
deleteJob
times out and becomes null?
r

r2vq

05/13/2019, 7:31 PM
It’ll evaluate to false if
isCancelled
is
true
or
null
. I think what I meant to put was
Copy code
if (deleteJob?.isCancelled != false) {
    // do something
}
That will evaluate to false if
deleteJob
is
null
as well
h

Hexa

05/14/2019, 7:07 PM
cool will try that thanks @r2vq
4 Views