Untitled
unknown
plain_text
12 days ago
1.1 kB
4
Indexable
fun ViewModel.launch( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, onError: (Throwable) -> Unit = {}, finallyBlock: () -> Unit = {}, block: suspend CoroutineScope.() -> Unit, ): Job { return viewModelScope.launch(context = context, start = start) { try { block() } catch (exception: Exception) { if (exception is CancellationException) throw exception Timber.e(exception) onError(exception) } finally { finallyBlock() } } } fun <T> ViewModel.async( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, onError: (Throwable) -> Unit = {}, block: suspend CoroutineScope.() -> T, ): Deferred<T?> { return viewModelScope.async(context = context, start = start) { try { block() } catch (exception: Exception) { if (exception is CancellationException) throw exception Timber.e(exception) onError(exception) null } } }
Editor is loading...
Leave a Comment