I have an exceptionHandler passed to the the scope...
# coroutines
k
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
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
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
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
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