I came across an issue with KMP where I have a sim...
# getting-started
s
I came across an issue with KMP where I have a simple coroutine in a kotlin file. As simple as GlobalScope.launch) { repeat(5){ delay(2000) println("TEST $it") } } When Using KMP to generate a JS file. This JS is then executed on IOS simulator inside a webview and it seems to work fine and runs the coroutine. But when I try to run the same JS file inside of a webview on physical IOS device (IPhone 17.5.0) it only runs once and then stops. Also, On physical IOS device If I run the same script on safari it works, Only when this script is launched in a webview inside of an app it doesnt work. I cannot see any crashes or so, It just runs once and stops. Even without a repeat if i just call GlobalScope.launch{ delay(2000) println("Test 1") delay(2000) println("Test 2") } This still only runs once on physical IOS device. Some help would be brilliant.
j
Is anything ensuring that the JavaScript snippet keeps running? I'm not sure about your specific use case, but here you're using
GlobalScope
, which means you're escaping structured concurrency. When using such a snippet in a
main()
function, the program may terminate before the coroutine can finish running. If you're relying on race conditions, you might notice different behaviors in different environments.
s
I am calling this coroutine to start on a button click from an html not on
main()
About ensuring the JavaScript snippet keeps running, I am not sure how can I do that. Would you be able to advice me on this? What would be the best way to run a coroutine for KMP js?
@Joffrey one thing I forgot to mention. On physical iPhone if I copy generated .js file to iOS project and compile, run in webview it actually works fine. Issue comes if I inject this JS during runtime. I have tried with CoroutineScope with different dispatchers and issue still exists.
An update. This issue only occurs if I try to run JavaScript without a webview for IOS app. If I have a webview running even if it's 1px x 1px it will work. As long as there's a webview in view hierarchy
j
Sorry Salam for the delay. I'm afraid I have very limited knowledge of this domain so I won't be able to help here (don't be fooled by the JetBrains tag, I'm just a humble Kotlin user that happens to work on another JetBrains project 😄)
❤️ 1
s
Thanks for help anyways 🙂.