当前位置:网站首页>[kotlin] keyword suspend learning of thread operation and async understanding

[kotlin] keyword suspend learning of thread operation and async understanding

2022-06-26 22:33:00 ChrisEighteen18

background

stay kotlin To learn some of the methods used and practical applications :

  • suspend How to use it in the end ?
package im.zego.takeaguess.logic.sdk.interfaces

import android.app.Application
import im.zego.zim.entity.ZIMMessage
import kotlinx.coroutines.flow.FlowCollector

interface IZIMManager {
    

    fun init(application: Application)

    fun unInit()

    /** *  Sign in  * * @param userId * @param userName * @param token */
    suspend fun login(userId: String, userName: String, token: String): Result<Unit>

    /** *  Log out  * */
    fun logout()

    /** *  Create and join a room  * * @param roomId * @param roomName */
    suspend fun createRoom(roomId: String, roomName: String): Result<Unit>

    /** *  Join a room  * * @param roomId */
    suspend fun joinRoom(roomId: String): Result<Unit>

    /** *  Leave the room  * * @param roomId */
    suspend fun logoutRoom(roomId: String): Result<Unit>

    /** *  Observe room message changes  * * @param collector */
    suspend fun observeRoomMessage(collector: FlowCollector<List<ZIMMessage>>)
}

Answer questions and solve doubts

suspend Use of functions
The main thing is to learn a async Deep understanding of asynchronous processing , The Chinese meaning of this keyword is : Hang . This makes me think directly about the thread hang , Release resources when you need them . Actually in kotlin China is much the same , However, the method used is quite special ; and java It's different .

 Insert picture description here
After realizing the above interface Interface It can be used as shown in the following code ;
The basic logic is to use keywords async Asynchronous processing . This saves resources and time .

  val loginZimRoomDef = async(Dispatchers.IO) {
    
                zimManager.createRoom(
                    roomId = joinRoomResp.roomInfo.roomId,
                    roomName = joinRoomResp.roomInfo.name
                )
            } //  establish ZIM room 
            val loginExpressRoomDef = async(Dispatchers.IO) {
    
                expressManager.loginRoom(
                    roomId = joinRoomResp.roomInfo.roomId,
                    userId = AccountStore.userId,
                    userName = AccountStore.userName,
                    token = token
                )
            } //  Get into Express room 
            val initCopyrightedMusicDef = async(Dispatchers.IO) {
    
                copyrightMusicManager.init(
                    userId = AccountStore.userId, userName = AccountStore.userName
                )

The above code can implement multiple initialization methods at the same time .
Please refer to for more usage Composing suspending functions -kotlin Official website

原网站

版权声明
本文为[ChrisEighteen18]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206262222196098.html