【Android Kotlin】Kotlin专题学习

文章目录

  • [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()
        }
    }
}