readdle / swift-android-toolchain

MIT License
435 stars 32 forks source link

DispatchQueue.main does not work #35

Closed OlofT closed 3 years ago

OlofT commented 3 years ago

It seems something is blocking the main thread, calling this from Android, into Swift:

        print("In main? \(Thread.isMainThread)")  //true
        DispatchQueue.main.async {

            //never called
            print("MAIN async")
        }

I imagine this is due to Android owning the main thread and running its loop there all the time, so the question is perhaps - how to get it to yield for Swift at regular intervals? Or how to make Swift's main process itself on another thread?

OlofT commented 3 years ago

The solution is to start the Swift process in another thread:

import kotlin.concurrent.thread
...
thread {
    System.loadLibrary("WeatherCoreBridge")
    JavaSwift.init()
    ....
}