Replies: 8 comments
-
写的很不错,冒昧问下,你这画图工具是直接用iPad画图工具画的么 |
Beta Was this translation helpful? Give feedback.
-
感谢留言😄~ 插图用的是 https://excalidraw.com/ 这个工具。 |
Beta Was this translation helpful? Give feedback.
-
suspend 函数提供的约定:调用这个函数不会阻塞线程,函数内的子程序执行完毕以后函数才会返回,控制流回到调用方。suspend 函数不应该有开启异步任务的副作用。 |
Beta Was this translation helpful? Give feedback.
-
suspend 函数内部封装了回调,比如下面的例子: suspend fun requestToken(): String
suspend fun createPost(token: String, item: Item): Post
suspend fun processPost(post)
suspend fun postItem(item: Item) {
val token = 🏹 requestToken()
val post = 🏹 createPost(token, item)
🏹 processPost(post)
} 可以看成等价于这样回调的写法: // 三个基于回调的 API
fun requestToken(block: (String) -> Unit)
fun createPost(token: String, item: Item, block: (Post) -> Unit))
fun processPost(post: Post)
fun postItem(item: Item) {
requestToken { token ->
createPost(token, item) { post ->
processPost(post)
}
}
} 在这两个例子中三个步骤 假设 suspend 封装的是接口请求,比如 Retrofit (OkHttp),最终会调用异步的 所以说要真正达到不阻塞调用方线程的效果,最终需要依赖异步的接口调度到别的线程去执行。 《理解 Kotlin 的 suspend 函数》 这篇文章中有关于 suspend 更多介绍~ |
Beta Was this translation helpful? Give feedback.
-
写的真是很好,相当实用! |
Beta Was this translation helpful? Give feedback.
-
写得可太好了 大佬 |
Beta Was this translation helpful? Give feedback.
-
https://blog.yujinyan.me/posts/kotlin-coroutine-context-scope/
Beta Was this translation helpful? Give feedback.
All reactions