ahulyk
03/13/2019, 2:31 PMclass 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
}
}
}
}
gildor
03/13/2019, 3:58 PMahulyk
03/14/2019, 8:39 AMgildor
03/14/2019, 9:14 AMWhat is the actual difference between SupervisorJob() and CoroutineScope constructor?This question doesn’t make make sense, Job and Scope are different things
ahulyk
03/14/2019, 9:56 AMCoroutineScope
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()
}
gildor
03/14/2019, 9:57 AMwhy the following solution should work?Could you elaborate, what exactly means “should work”, does it work for you?
ahulyk
03/14/2019, 10:04 AMgildor
03/14/2019, 10:14 AMahulyk
03/14/2019, 10:15 AMgildor
03/14/2019, 10:16 AMor 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
ahulyk
03/14/2019, 10:17 AM