docs: ViewModelKtx.kt 完善详细注释

This commit is contained in:
Quyunshuo
2023-02-13 23:29:43 +08:00
parent c8418aaa37
commit 4fe1bcf472

View File

@ -7,6 +7,23 @@ import kotlinx.coroutines.*
/** /**
* 开启一个线程调度模式为[Dispatchers.IO]的协程 有默认的异常处理器 * 开启一个线程调度模式为[Dispatchers.IO]的协程 有默认的异常处理器
* *
* **sample:**
* ```
* class SampleViewModel : ViewModel() {
*
* fun sample() {
* launchIO {
* // 协程体
* }
* launchIO(exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable ->
* // exception handling
* }) {
* // 协程体
* }
* }
* }
* ```
*
* @receiver ViewModel * @receiver ViewModel
* *
* @param exceptionHandler CoroutineExceptionHandler 异常处理器 * @param exceptionHandler CoroutineExceptionHandler 异常处理器
@ -14,7 +31,7 @@ import kotlinx.coroutines.*
* @return Job * @return Job
*/ */
fun ViewModel.launchIO( fun ViewModel.launchIO(
exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable -> exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
throwable.printStackTrace() throwable.printStackTrace()
}, },
block: suspend CoroutineScope.() -> Unit block: suspend CoroutineScope.() -> Unit
@ -23,6 +40,23 @@ fun ViewModel.launchIO(
/** /**
* 开启一个线程调度模式为[Dispatchers.Default]的协程 有默认的异常处理器 * 开启一个线程调度模式为[Dispatchers.Default]的协程 有默认的异常处理器
* *
* **sample:**
* ```
* class SampleViewModel : ViewModel() {
*
* fun sample() {
* launchDefault {
* // 协程体
* }
* launchDefault(exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable ->
* // exception handling
* }) {
* // 协程体
* }
* }
* }
* ```
*
* @receiver ViewModel * @receiver ViewModel
* *
* @param exceptionHandler CoroutineExceptionHandler 异常处理器 * @param exceptionHandler CoroutineExceptionHandler 异常处理器
@ -30,7 +64,7 @@ fun ViewModel.launchIO(
* @return Job * @return Job
*/ */
fun ViewModel.launchDefault( fun ViewModel.launchDefault(
exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable -> exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
throwable.printStackTrace() throwable.printStackTrace()
}, },
block: suspend CoroutineScope.() -> Unit block: suspend CoroutineScope.() -> Unit
@ -39,6 +73,23 @@ fun ViewModel.launchDefault(
/** /**
* 开启一个线程调度模式为[Dispatchers.Main]的协程 有默认的异常处理器 * 开启一个线程调度模式为[Dispatchers.Main]的协程 有默认的异常处理器
* *
* **sample:**
* ```
* class SampleViewModel : ViewModel() {
*
* fun sample() {
* launchMain {
* // 协程体
* }
* launchMain(exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable ->
* // exception handling
* }) {
* // 协程体
* }
* }
* }
* ```
*
* @receiver ViewModel * @receiver ViewModel
* *
* @param exceptionHandler CoroutineExceptionHandler 异常处理器 * @param exceptionHandler CoroutineExceptionHandler 异常处理器
@ -46,7 +97,7 @@ fun ViewModel.launchDefault(
* @return Job * @return Job
*/ */
fun ViewModel.launchMain( fun ViewModel.launchMain(
exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable -> exceptionHandler: CoroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
throwable.printStackTrace() throwable.printStackTrace()
}, },
block: suspend CoroutineScope.() -> Unit block: suspend CoroutineScope.() -> Unit