文章目录
- [1. 内联函数](#1. 内联函数)
-
- [1.2 案例分析](#1.2 案例分析)
1. 内联函数
1.2 案例分析
kotlin
open class BaseRepository {
suspend inline fun <reified T : Any> launchRequest(
crossinline block: suspend () -> CResponse<T>,
noinline onSuccess: ((T?) -> Unit)? = null,
noinline onError: ((Exception)-> Unit) ? = null,
noinline onComplete: (() -> Unit)? = null ){
try {
val response = block()
onSuccess?.invoke(response?.data)
} catch (e: Exception) {
e.printStackTrace()
when (e) {
is UnknownHostException -> {
//...
}
//... 各种需要单独处理的异常
is ConnectException -> {
//...
}
else -> {
//...
}
}
onError?.invoke(e)
}finally {
onComplete?.invoke()
}
}
}

