From 4fe1bcf472a8748c27ee960fa757ba3c80ac8feb Mon Sep 17 00:00:00 2001 From: Quyunshuo Date: Mon, 13 Feb 2023 23:29:43 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20ViewModelKtx.kt=20=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E8=AF=A6=E7=BB=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ktx/{ViewModelExt.kt => ViewModelKtx.kt} | 57 ++++++++++++++++++- 1 file changed, 54 insertions(+), 3 deletions(-) rename lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/ktx/{ViewModelExt.kt => ViewModelKtx.kt} (59%) diff --git a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/ktx/ViewModelExt.kt b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/ktx/ViewModelKtx.kt similarity index 59% rename from lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/ktx/ViewModelExt.kt rename to lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/ktx/ViewModelKtx.kt index 09b6fac..5475d57 100644 --- a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/ktx/ViewModelExt.kt +++ b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/ktx/ViewModelKtx.kt @@ -7,6 +7,23 @@ import kotlinx.coroutines.* /** * 开启一个线程调度模式为[Dispatchers.IO]的协程 有默认的异常处理器 * + * **sample:** + * ``` + * class SampleViewModel : ViewModel() { + * + * fun sample() { + * launchIO { + * // 协程体 + * } + * launchIO(exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable -> + * // exception handling + * }) { + * // 协程体 + * } + * } + * } + * ``` + * * @receiver ViewModel * * @param exceptionHandler CoroutineExceptionHandler 异常处理器 @@ -14,7 +31,7 @@ import kotlinx.coroutines.* * @return Job */ fun ViewModel.launchIO( - exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable -> + exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { _, throwable -> throwable.printStackTrace() }, block: suspend CoroutineScope.() -> Unit @@ -23,6 +40,23 @@ fun ViewModel.launchIO( /** * 开启一个线程调度模式为[Dispatchers.Default]的协程 有默认的异常处理器 * + * **sample:** + * ``` + * class SampleViewModel : ViewModel() { + * + * fun sample() { + * launchDefault { + * // 协程体 + * } + * launchDefault(exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable -> + * // exception handling + * }) { + * // 协程体 + * } + * } + * } + * ``` + * * @receiver ViewModel * * @param exceptionHandler CoroutineExceptionHandler 异常处理器 @@ -30,7 +64,7 @@ fun ViewModel.launchIO( * @return Job */ fun ViewModel.launchDefault( - exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable -> + exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { _, throwable -> throwable.printStackTrace() }, block: suspend CoroutineScope.() -> Unit @@ -39,6 +73,23 @@ fun ViewModel.launchDefault( /** * 开启一个线程调度模式为[Dispatchers.Main]的协程 有默认的异常处理器 * + * **sample:** + * ``` + * class SampleViewModel : ViewModel() { + * + * fun sample() { + * launchMain { + * // 协程体 + * } + * launchMain(exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable -> + * // exception handling + * }) { + * // 协程体 + * } + * } + * } + * ``` + * * @receiver ViewModel * * @param exceptionHandler CoroutineExceptionHandler 异常处理器 @@ -46,7 +97,7 @@ fun ViewModel.launchDefault( * @return Job */ fun ViewModel.launchMain( - exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable -> + exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { _, throwable -> throwable.printStackTrace() }, block: suspend CoroutineScope.() -> Unit