

The easiest way to get around this is using the Stately-common library. This function and other related to freezing are only available on Kotlin / Native which makes them unavailable in the common code. This function throws a FreezingException when the class gets frozen, with a verbose stack trace that helps debug where the freeze started. To prevent this, Kotlin / Native exposes a function called ensureNeverFrozen(). If this happens, the moment that a mutation will take place an InvalidMutabilityException will be thrown. Preventing accidental freezing of classesĬlasses which are mutable or classes that have mutable dependencies cannot be frozen. This new threading model forced us to separate the long-lasting logic into self-contained classes which switch the threads and the return a result without freezing any unwanted classes. Big shout out to Kevin Galligan and Touchlab for creating so many resources around the topic.

The oversimplified version is that when referencing a class from a different thread the whole class becomes frozen along with its dependencies, meaning that they cannot be mutated / changed anymore.Īt first, it seemed counterproductive and a burden, but after reading some resources around the topic, we've found that this restriction makes the code more predictable when it comes to multi-threading. Kotlin / Native is very restrictive when it comes to sharing classes between threads, whereas Kotlin / JVM is not. This is probably the biggest hurdle we've faced when working on the POC. Kotlin Multiplatform: Kotlin / Native different threading model: We feel that this gives us more control of how we integrate the platforms with Kotlin Multiplatform. This means that the Presentation layer is also implemented on both platforms. The implementation would still call the use cases underneath, however such an interface would help with creating Kotlin / Native wrappers (explained later). However, we are thinking that maybe exposing an interface that groups related features might be beneficial.

The platforms communicate with the KMP module using use cases. The Touchlab KaMPKit project was used as the starter for the Proof of Concept.

This article contains information about our experiences with Kotlin Multiplatform, which hopefully might be useful for others. This article was originally posted on my blog.Ĭurrently, we're planning a rebuild of our apps using the Kotlin Multiplatform, Jetpack Compose and Swift UI for Android and iOS respectively.
