<@UHAJKUSTU> What is the simplest approach for man...
# decompose
e
@Arkadii Ivanov What is the simplest approach for managing components in iOS not in the Swift code, but in the Kotlin multiplatform code? I would like to avoid adding Swift as soon as possible and wonder if I can create a RootComponent just with kotlin.
For example in Android I have the following code:
Copy code
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val rootComponent =
            DefaultRootComponent(
                componentContext = defaultComponentContext(),
            )

        enableEdgeToEdge()

        setContent {
            App(rootComponent = rootComponent)
        }
    }
}
in iOS I only have this right now:
Copy code
fun MainViewController() = ComposeUIViewController {
    App()
}
Is this a good approach?:
Copy code
@OptIn(ExperimentalDecomposeApi::class)
fun MainViewController(): UIViewController {
    val rootComponent =
        DefaultRootComponent(
            componentContext = DefaultComponentContext(lifecycle = ApplicationLifecycle()),
        )

    return ComposeUIViewController {
        App(rootComponent = rootComponent)
    }
}
a
Yes, this approach is pretty good. In this case you also don't need to export anything from your iOS framework.
But you have to take extra care to avoid leaking the component. It should be the only instance in your app. I.e. it becomes easy to call
MainViewController
function multiple times (e.g. in different parts of the app) without realising that it attaches the component to the application lifecycle.
👍 1
e
Thank you! I will need to find out who and when is calling
MainViewController
function. I am new to iOS development with KMM and don’t see any references.. =)
👍 1