gpt4 book ai didi

kotlin - 无法在 Kotlin Multiplatform 中访问 fixedRateTimer

转载 作者:行者123 更新时间:2023-12-02 13:03:22 24 4
gpt4 key购买 nike

我正在开发一个 Kotlin 多平台项目。我正在尝试使用计时器和倒数计时器,但我无法访问 kotlin.concurrent.fixedRateTimerimport kotlin.concurrent.timercommonMain模块。
enter image description here
然而 kotlin.concurrent可用:
enter image description here
这是根build.gradle :

plugins {
kotlin("multiplatform")
id("com.android.library")
id("kotlin-android-extensions")
}

// ...

kotlin {
//...
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.10")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.4.10")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9")
//...
}
}
//...
}
}
我想知道是否甚至可以在那里使用这些方法。如果没有,如何在 commonMain 中编写计时器和倒数计时器?模块?
我尝试使用 Coroutines实现相同的功能但由于它们不精确而失败:
    fun doAfter(delay: Long, action: () -> (Unit)) = launch {
delay(delay)
action.invoke()
}

fun countdown(time: Long, tick: Long, onTick: () -> (Unit), onFinish: () -> (Unit)) = launch {
val ticks = (time / tick).toInt()
repeat(ticks) {
onTick()
delay(tick)
}
onFinish()
}

最佳答案

正如 Qaz 所说,您尝试在公共(public)代码中使用的函数是 JVM only .
通常在 KMP 中,当您仍然没有框架内置的通用功能时,您可以采用不同的方法:

  • 使用其他人的图书馆(例如 moko-time ) - 搜索图书馆的最佳位置是 here .
  • 通过 expect / actual 使用 native 框架类机制

  • 只是为了给你和可以做什么的例子(不确定这是否适合你或是否适合你的需求。这只是为了让你朝着正确的方向前进,最重要的是我写的东西根本无法用于生产[-;)
    commonMain:Timer.kt
    expect class KMMTimer(
    name: String? = null,
    interval: Long,
    delay: Long,
    action: () -> Unit
    ) {
    val name: String?
    val interval: Long
    val delay: Long

    fun start()
    fun cancel()
    fun isRunning(): Boolean
    }

    androidMain:Timer.kt
    import java.util.*
    import kotlin.concurrent.fixedRateTimer

    actual class KMMTimer actual constructor(
    actual val name: String?,
    actual val interval: Long,
    actual val delay: Long,
    action: () -> Unit
    ) {
    private var timer: Timer? = null
    private val action = action

    actual fun start() {
    if (!isRunning()) {
    timer = fixedRateTimer(
    name = name,
    initialDelay = delay,
    period = interval
    ) {
    action()
    }
    }
    }

    actual fun cancel() {
    timer?.cancel()
    timer = null
    }

    actual fun isRunning(): Boolean {
    return timer != null
    }
    }
    iosMain:Timer.kt
    import platform.Foundation.NSDate
    import platform.Foundation.NSRunLoop
    import platform.Foundation.NSRunLoopCommonModes
    import platform.Foundation.NSTimer

    actual class KMMTimer actual constructor(
    actual val name: String?,
    actual val interval: Long,
    actual val delay: Long,
    action: () -> Unit
    ) {
    private var timer: NSTimer? = null
    private var action = action

    actual fun start() {
    if (!isRunning()) {
    timer = NSTimer(
    fireDate = NSDate(
    NSDate().timeIntervalSinceReferenceDate + (delay.toDouble() / 1000)
    ),
    interval = (interval.toDouble() / 1000),
    repeats = true,
    block = {
    action()
    }
    )
    timer?.let {
    NSRunLoop.currentRunLoop().addTimer(it, NSRunLoopCommonModes)
    }
    }
    }

    actual fun cancel() {
    timer?.invalidate()
    timer = null
    }

    actual fun isRunning(): Boolean {
    return timer != null
    }
    }

    关于kotlin - 无法在 Kotlin Multiplatform 中访问 fixedRateTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64458126/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com