在kotlin中完成异步函数后,如何执行另一个函数?

在kotlin中完成异步函数后,如何执行另一个函数?,kotlin,asynchronous,Kotlin,Asynchronous,我正在实例化以下变量: phoneViewModel = ViewModelProvider(this).get(PhoneViewModel::class.java).also {it.initialRead()} initialRead()调用另一个异步检索数据的函数。当我在应用程序中使用phoneViewModel变量时,应用程序崩溃,因为initialRead()尚未完成。“异步”实例化完成后,如何执行另一个函数,例如usePhoneViewModel() public fun ini

我正在实例化以下变量:

phoneViewModel = ViewModelProvider(this).get(PhoneViewModel::class.java).also {it.initialRead()}
initialRead()调用另一个异步检索数据的函数。当我在应用程序中使用phoneViewModel变量时,应用程序崩溃,因为initialRead()尚未完成。“异步”实例化完成后,如何执行另一个函数,例如usePhoneViewModel()

public fun initialRead(onError: ((errorMessage: String) -> Unit)? = null) {
    if (!isDownloadError) {
        repository.initialRead(
            Action0 { isDownloadError = false},
            Action1 { error ->
                isDownloadError = true
                onError?.let {
                    val resources = getApplication<Application>().resources
                    onError.invoke(resources.getString(R.string.read_failed_detail))
                }
            }
        )
    }
}
public fun initialRead(onError:((errorMessage:String)->Unit)?=null){
如果(!isDownloadError){
repository.initialRead(
Action0{isDownloadError=false},
操作1{错误->
isDownloadError=true
一个错误?让我来{
val resources=getApplication().resources
onError.invoke(resources.getString(R.string.read\u failed\u detail))
}
}
)
}
}
并在回购协议中进行首读

fun initialRead(successHandler: Action0?, failureHandler: Action1<RuntimeException>) {
    relatedEntities.clear()

    if (initialReadDone && entities.size > 0) {
        observableEntities.setValue(entities)
        return
    }

    var dataQuery = DataQuery().from(entitySet)
    if (orderByProperty != null) {
        dataQuery = dataQuery.orderBy(orderByProperty, SortOrder.ASCENDING)
    }

    zGW_EXT_SHIP_APP_SRV_Entities.executeQueryAsync(dataQuery,
        Action1 { queryResult ->
            val entitiesRead = convert(queryResult.entityList)
            entities.clear()
            entities.addAll(entitiesRead)
            initialReadDone = true
            observableEntities.value = entitiesRead
            successHandler?.call()
        },
        failureHandler,
        httpHeaders)
}
fun initialRead(successHandler:Action0?,failureHandler:Action1){
relatedEntities.clear()
if(initialReadDone&&entities.size>0){
observableEntities.setValue(实体)
返回
}
var dataQuery=dataQuery().from(entitySet)
if(orderByProperty!=null){
dataQuery=dataQuery.orderBy(orderByProperty,SortOrder.ASCENDING)
}
zGW_EXT_SHIP_APP_SRV_Entities.executeQueryAsync(数据查询,
操作1{queryResult->
val entitiesRead=convert(queryResult.entityList)
实体.清除()
entities.addAll(entitiesRead)
initialReadDone=true
observableEntities.value=实体读取
successHandler?.call()
},
故障处理程序,
HttpHeader)
}

鉴于此功能,我认为您不能。将
onSuccess
参数添加到
initialRead
,例如:

public fun initialRead(onSuccess: (() -> Unit)? = null, onError: ((errorMessage: String) -> Unit)? = null) {
    if (!isDownloadError) {
        repository.initialRead(
            Action0 { 
                isDownloadError = false
                onSuccess?.invoke()
            },
            Action1 { error ->
                isDownloadError = true
                onError?.let {
                    val resources = getApplication<Application>().resources
                    onError.invoke(resources.getString(R.string.read_failed_detail))
                }
            }
        )
    }
}

请更准确地说明
initialRead
是异步的:它是否返回Java
Future
CompletableFuture
或其他类似类型?它是Kotlin
suspend
功能吗?还有什么?用@AlexeyRomanov更新谢谢!但从未调用usePhoneViewModel()。有什么想法吗?也许你是在
if(initialReadDone&&entities.size>0)
的情况下?但这只是一个猜测,我建议您自己尝试调试它。刚刚调试,它进入executeQueryAsync和succesHandler?调用,但我的函数usePhoneViewModel没有调用hmmm
ViewModelProvider(this).get(PhoneViewModel::class.java).also {
    it.initialRead(onSuccess = { usePhoneViewModel() })
}