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

kd

11/05/2019, 7:00 PM
I have an exceptionHandler passed to the the scope.launch and based on a certain exception I want to stop the coroutine and then restart it. Is this the correct way to achieve it?
o

octylFractal

11/05/2019, 7:04 PM
I don't think you need to cancel the job, because when an exception occurs that has already happened (you can't stop the cancellation by using a handler). additionally, you can't reuse a job once it's been cancelled/finished
k

kd

11/05/2019, 7:07 PM
So would I need to create a new instance of the job? job = Job() and/or create a new instance of the scope? scope = CoroutineScope(dispatcher + job) and then call the coroutineMethod?
o

octylFractal

11/05/2019, 7:09 PM
I don't think you need
job
at all -- create your scope with
SupervisorJob()
so it doesn't get cancelled on exception. you don't need the
isActive
check for the
while
loop because
delay
is already cancellable iirc (check docs), and you don't need to cancel a job in your handler
k

kd

11/05/2019, 7:10 PM
Regarding delay, I want to run the coroutine every 4 seconds to make the api call.
I want the coroutine to stop execution after an exception, dosomething and then restart
5 Views