d

    David Hart

    1 year ago
    I'm trying to access
    Thread.currentThread()
    from a Kotlin Mobile Multiplatform project to check that I am in the main thread, but it doesn't seem accessible. Are my missing something? Is there another way?
    Arkadii Ivanov

    Arkadii Ivanov

    1 year ago
    There is no such a thing like Thread in common source set. But you can expect/actual.
    Kris Wong

    Kris Wong

    1 year ago
    Thread
    is a Java API
    e

    edenman

    1 year ago
    expect fun isMainThread(): Boolean
    actual fun isMainThread(): Boolean = Looper.getMainLooper() == Looper.myLooper()
    (assuming you’ve got an android sourceset ofc)
    d

    David Hart

    1 year ago
    Thanks for the answers!
    m

    Michal Klimczak

    1 year ago
    If you're trying to do the same
    actual
    for iOS, it would be something like this
    import platform.Foundation.NSThread
    
    actual fun currentThreadName() = "iOS thread ${NSThread.currentThread()} is main: ${NSThread.isMainThread()}"