https://kotlinlang.org logo
j

Jan

09/11/2023, 3:52 PM
Hi, is it correct that MainViewController is called twice of the compose-multiplatform-ios-android template? More in thread:
If I run the app for iOS the code within main.ios.kt is called twice:
Copy code
fun MainViewController() = ComposeUIViewController {

    println("MainViewController")

    App()
}
and my log output is:
Copy code
Debugger attached to process 44790
MainViewController
MainViewController
l

Landry Norris

09/11/2023, 4:49 PM
Composable functions can be called as many times as the Composer decides is necessary. Any side effects should be using LanchedEffect or some other Effect.
j

Jan

09/12/2023, 7:19 AM
hm ... but thats not a composable function.
ok, got it. my MainViewController was wrong:
Copy code
fun MainViewController(): UIViewController {
    
    println("MainViewController")

    return ComposeUIViewController {
        App()
    }

}
s

Shoaib khalid

09/28/2023, 4:39 PM
@Jan i'm facing a similar problem. Did you also face it? https://kotlinlang.slack.com/archives/C0346LWVBJ4/p1695918487627549
l

Landry Norris

09/28/2023, 4:44 PM
The ComposeUIViewController block is a composable lambda. Composable lambdas can be called any time the composer decides to. I know the ViewController handles some iOS state internally, so it's possible it decides to update some theme or something after the first composition, triggering another. This is what you are seeing, and is within allowed behavior for the composer.
3 Views