Kotlin Multiplatform. Working with multithreading

There are plenty of basic examples of KMM applications on the web, so we’ll look at something closer to our day-to-day development tasks, such as how to implement a multi-threaded application on the Kotlin Multiplatform.

To begin with, a little introductory information. If you are already familiar with Kotlin Multiplatform, follow the example below. If you want to go immediately to the advanced example, then you here.

The main idea of ​​KMM, as well as other cross-platform technologies, is to optimize development by writing code once and then using it on different platforms.

According to the JetBrains concept, the Kotlin Multiplatform is not a framework. This is the SDK, which allows you to create modules with common code that connect to native applications.

Platform-specific versions of Kotlin are used to interact with platforms: Kotlin / JVM, Kotlin / JS, Kotlin / Native. These versions include Kotlin language extensions, as well as platform-specific libraries and tools. The module written in Kotlin is compiled into JVM bytecode for Android and LLVM bytecode for iOS.

The module (Shared, Common) contains reusable business logic. The iOS / Android platform modules to which Shared / Common is connected either use the written logic directly or implement their implementation depending on the platform’s features.

General business logic may include:

network services;
services for work with a DB;
data models.

It can also include architectural components of the application that do not directly include the UI, but interact with it:

ViewModel;
Presenter;
Interactors, etc.