Coroutine doens't start?

血红的双手。 提交于 2019-12-24 06:21:46

问题


Based on this post throttleFirst function:

fun <T> throttleFirst(
    skipMs: Long = 700L,
    scope: CoroutineScope = viewModelScope,
    action: (T) -> Unit
): (T) -> Unit {
    var throttleJob: Job? = null
    return { param: T ->
        if (throttleJob?.isCompleted != false) {
            throttleJob = coroutineScope.launch {
                destinationFunction(param)
                delay(skipMs)
            }
        }
    }
}

I'm using it like this:

View

<Button
    android:onClick="@{viewModel.myClickListener}"
.../>

ViewModel:

fun myClickListener() = View.OnClickListener { _ ->
    throttleClick(clickAction = {
        //do things
    })
}

BaseViewModel:

protected fun throttleClick(millis: Long = 700L, clickAction: (Unit) -> Unit): (Unit) -> Unit  {
    throttleFirst(millis, scope = viewModelScope, action = clickAction)
}

But nothing happens, the clickAction is not reached. While debugging, step-by-step ends when it hits return { param: T -> and that returning function (throttleJob?.isCompleted... code) is never called.
What am I doing wrong?

EDIT with the help from Patrick the final solution is:

ViewModel

private val myThrottleClick = throttleClick(clickAction = {
    //do things
})

fun myClickListener() = View.OnClickListener { myThrottleClick(Unit) }

BaseViewModel

protected fun throttleClick(millis: Long = 700L, clickAction: (Unit) -> Unit): (Unit) -> Unit {
    return throttleFirst(millis, action = clickAction)
}

回答1:


Your throttleFirst function makes a click listener, so you must store it in a val outside of your click listeners scope. i.e.

val clickListener = throttleFirst { doStuff() }

fun myClickListener() = View.OnClickListener { _ -> clickListener() }

You may be able to do away with the myClickListener function entirely and just reference clickListener in xml.



来源:https://stackoverflow.com/questions/59413001/coroutine-doenst-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!