Sangeet
10/29/2018, 11:09 AMCoroutineExceptionHandler
to catch exception that is written inside a function. While calling that function again it is actually throwing previous Exception without executing any coroutines inside that. Can anybody help me with this.marstran
10/29/2018, 11:16 AMSangeet
10/29/2018, 11:33 AMfun <T, U> CoroutineScope.merge(
fun1: suspend () -> T,
fun2: suspend () -> U,
doOnSuccess: suspend (CoroutineContext, T, U) -> Unit,
doOnError: () -> Unit,
doOnNoInternet: (() -> Unit)? = null,
doOnBefore: (() -> Unit)? = null
) {
val exceptionHandler: CoroutineContext = CoroutineExceptionHandler { _, throwable ->
doOnBefore?.invoke()
if (throwable is NoInternetException)
doOnNoInternet?.invoke()
else
doOnError()
}
val job1 = async(<http://Dispatchers.IO|Dispatchers.IO> + exceptionHandler) { fun1.invoke() }
val job2 = async(<http://Dispatchers.IO|Dispatchers.IO> + exceptionHandler) { fun2.invoke() }
launch(<http://Dispatchers.IO|Dispatchers.IO> + exceptionHandler) {
val result1 = job1.await()
val result2 = job2.await()
withContext(Dispatchers.Main + exceptionHandler) {
doOnBefore?.invoke()
doOnSuccess(exceptionHandler, result1, result2)
}
}
}
Sangeet
10/29/2018, 12:48 PMthis.job = Job()
this.coroutineContext = job + Dispatchers.Default
Sangeet
10/29/2018, 12:48 PMSangeet
10/29/2018, 12:50 PM