https://kotlinlang.org logo
Title
a

ahulyk

03/13/2019, 2:31 PM
Hi all! What is the difference between Job and SupervisorJob in case of Exception Handling?
class Presenter() : CoroutineScope {
        private val job = Job()

        override val coroutineContext: CoroutineContext
            get() = job + Dispatchers.Main

        override fun onDestroy() {
            job.cancel()
        }

        fun loadData() {
            launch {
                try {
                    //loadData().async()
                } catch (e: Exception) {
                    //handle exception
                }
            }

        }
    }
class Presenter() : CoroutineScope {
        private val job = SupervisorJob()

        override val coroutineContext: CoroutineContext
            get() = job + Dispatchers.Main

        override fun onDestroy() {
            job.cancel()
        }

        fun loadData() {
            launch {
                try {
                    //loadData().async()
                } catch (e: Exception) {
                    //handle exception
                }
            }

        }
    }
g

gildor

03/13/2019, 3:58 PM
Did you read docs of SupervisorJob?
a

ahulyk

03/14/2019, 8:39 AM
Yes, looks like second example is the way to go in most cases.
g

gildor

03/14/2019, 9:14 AM
What is the actual difference between SupervisorJob() and CoroutineScope constructor?
This question doesn’t make make sense, Job and Scope are different things
In your case difference caused by different work of Job and SupervisorJob (and by default scope uses Job)
What is not clear for you? Why Job works like that? Or why do you have this exception?
a

ahulyk

03/14/2019, 9:56 AM
@gildor The only part that is still unclear for me is why the following solution should work? use the following shortcut via
CoroutineScope
constructor:
class UserViewModel: ViewModel, CoroutineScope by CoroutineScope(Dispatchers.Default) {
    override fun onCleared() {
        super.onCleared()
        cancel()
    }
}
The working solution is to create new Job(), after exception is caught:
CoroutineExceptionHandler { _, throwable ->
        //onError(throwable)
        job = Job()
    }
g

gildor

03/14/2019, 9:57 AM
job = Job() - this looks wrong
why the following solution should work?
Could you elaborate, what exactly means “should work”, does it work for you?
It’s pretty clear in your original example
but I don’t understand what you want to do in your last message
a

ahulyk

03/14/2019, 10:04 AM
The question is related to Roman Elizarov answer: https://github.com/Kotlin/kotlinx.coroutines/issues/996#issuecomment-468890079 SupervisorJob() works great but the second solution does not.
g

gildor

03/14/2019, 10:14 AM
But this not related to CoroutineExceptionHandler
a

ahulyk

03/14/2019, 10:15 AM
ok - now i got it 🙂
thanks!
g

gildor

03/14/2019, 10:16 AM
or use the following shortcut via CoroutineScope constructor:
I actually not sure what Romain means by this, this is exactly the same as version above, just more concise
but SupervisorJob is definitely related
a

ahulyk

03/14/2019, 10:17 AM
Yeap, SupervisorJob works great!