English 中文(简体)
Android 延迟
原标题:Android postDelayed vs Coroutines delay

我看到,这一内容有例,而Im 问,是否有客观理由使用Coroutines 实施这一条?

In case the link dies the code from example is below:

val watcher = object :TextWatcher{
    private var searchFor = ""

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        val searchText = s.toString().trim()
        if (searchText == searchFor)
            return

        searchFor = searchText

        launch {       
            delay(300)  //debounce timeOut
            if (searchText != searchFor)
                return@launch

            // do our magic here
        }
    }

    override fun afterTextChanged(s: Editable?) = Unit
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
}

EDIT: 为澄清,delay 目 录 可将Coroutines替换为Anderson Handler postDelayed的延迟。 不同之处在于,Coroutines将停止拖延,而Anderson Handler将履行拖延义务,将电文储存在即将在稍后时间执行的路口。 效果似乎相同,但不同之处在于拖延是如何实施的。 是否有客观原因,为什么在座各位会更好?

EDIT2:引人指出,在Coroutine 派遣者将使用像安乐器这样的东西。 Refer to this for more details. 这意味着,对简单拖延采用对应办法,是值得的。

问题回答

是的,在某些情况下,有客观理由选择<代码>delay。 如果你在拖延之前需要携带变数,或把它放在一起,使用拖延将使你更清楚的代码。 然而,就这一具体情况而言,这并不多加多,因为你在启动联合权利之后,当然会拖延。 但是,如果你认为《守则》更清洁,那么它就宁愿这样做。

从标准框架来看,所有校服都得到了执行。 在世系下,它永远是手工艺品和你可以直接使用的其他东西。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签