【问题标题】:What do i use now that Anko is deprecated?Anko 已被弃用,我现在用什么?
【发布时间】:2021-07-06 09:45:29
【问题描述】:

如何修复此代码中的弃用警告?或者,还有其他选择吗?

   runOnUiThread {

        doAsync {
             // room insert query
        }
   }
// anko Commons
implementation "org.jetbrains.anko:anko-commons:0.10.8"

【问题讨论】:

  • 你想做什么?在后台线程中运行插入查询并返回 UI?那么你应该使用协程或 RxJava。
  • 谢谢你,我已经计划使用协程,并且打算在我的项目中删除 anko lib

标签: android kotlin anko anko-component


【解决方案1】:

TLDR:老实说,我强烈推荐充分利用Kotlin

由于我不知道你Anko的确切目的,所以我会很笼统地回答。

Anko 很棒,但现在是时候继续前进了……尽管有多种替代方案,Kotlin 本身就是Anko 的“最佳”替代方案

  1. 您可以使用Kotlin's Extension functions制作来自Anko 的所有通用辅助方法。 (read more)

    像这样:

     fun MutableList<Int>.swap(index1: Int, index2: Int) {
         val tmp = this[index1] // 'this' corresponds to the list
         this[index1] = this[index2]
         this[index2] = tmp
     }
    
     val list = mutableListOf(1, 2, 3)
     list.swap(0, 2) // 'this' inside 'swap()' will hold the value of 'list'
    
  2. 您可以使用最先进的异步编程库Coroutine,它比RxJava 快得多。 (read more)

    fun main() = runBlocking { // this: CoroutineScope
     launch { // launch a new coroutine and continue
         delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
         println("World!") // print after delay
     }
     println("Hello") // main coroutine continues while a previous one is delayed
     } 
    
  3. 还有更多可以满足您的需求。

如果您有任何问题,请告诉我。

【讨论】:

    【解决方案2】:

    正如他们在github 的弃用页面上所说,还有更多替代方案。至于公共资源,他们提供了两个库,所有链接都是here,请记住,其中一些库可以被弃用,因为该页面的最后一次更新是在 2 年前。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      相关资源
      最近更新 更多