I'm trying to access `Thread.currentThread()` from...
# multiplatform
d
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?
a
There is no such a thing like Thread in common source set. But you can expect/actual.
k
Thread
is a Java API
e
Copy code
expect fun isMainThread(): Boolean
actual fun isMainThread(): Boolean = Looper.getMainLooper() == Looper.myLooper()
(assuming you’ve got an android sourceset ofc)
d
Thanks for the answers!
m
If you're trying to do the same
actual
for iOS, it would be something like this
Copy code
import platform.Foundation.NSThread

actual fun currentThreadName() = "iOS thread ${NSThread.currentThread()} is main: ${NSThread.isMainThread()}"