Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin Multiplatform. Advanced multithreading

Kotlin Multiplatform. Advanced multithreading

Anna Zharkova

July 03, 2021
Tweet

More Decks by Anna Zharkova

Other Decks in Programming

Transcript

  1. Introduction • Mobile developer since 2013 • Lead Mobile developer

    in «Usetech software» . • Native mobile development, iOS and Android (Swift/Objective-C, Kotlin/Java) cross-platform mobile development (Xamarin, Kotlin multiplatform) • Leading mobile team. Mentorship • Public speaking (Mobius Moscow 2020, AppsLive 2020) • Tutor in Otus (iOS advanced) • Writing articles about mobile development (especially iOS and KMM ) • Women Techmaker Ambassador since 2021 2
  2. Goals. Today discuss: • Kotlin Multiplatform and common concurrency •

    Coroutines, solving problems in Kotlin/Native • Concurrency without coroutines. Background Workers • Advanced Sharing • Flows & Kotlin/Native, iOS side 3
  3. Kotlin Multiplatform 5 • SDK, not framework • JVM for

    Android and LLVM for iOS • Total sharing (almost)
  4. Concurrency in Kotlin/Native 7 expect Dispatchers iOS: actual Dispatchers Android:

    actual Dispatchers Kotlin Native Kotlin JVM Kotlin JVM concurrency != Kotlin Native concurrency
  5. Networking as a case Common implementation (e.g. use Ktor) ->

    Coroutines Separated native -> Coroutines, native concurrency 8 Depends on app architecture
  6. Sample. Networking with Ktor and Coroutines 9 Common concurrency with

    Coroutines Ktor implements async work under its hood
  7. Coroutines vs GCD. Suspended 10 Suspended not blocking in iOS

    Suspended Kotlin easy transforms into Swift (since Kotlin Native 1.4)
  8. Problem of common architecture 11 Network service API service Common

    architecture iOS View Android View Common Work with coroutines Common architecture = common scope
  9. Setting up thread and contexts 12 Android/ iOS : Dispatchers.Default

    (Android) Dispatchers.Main – unique for platform
  10. Missed Dispatchers in iOS 16 No default dispatcher for iOS

    https://github.com/Kotlin/kotlinx.coroutines/issues/470
  11. Missed Dispatchers in iOS 17 Issue 462 still be not

    solved https://github.com/Kotlin/kotlinx.coroutines/issues/462
  12. CoroutineDispatcher for iOS 19 Not available to create working dispatcher

    bound to DispatchersQueue background We have to use Main as default (Dispatchers.Default not working)
  13. How to work with threads in Kotlin/Native 21 • Mutable

    == for one thread • Immutable == multiple thread In Kotlin JVM everything is already done
  14. How to work with threads in Kotlin/Native 22 Object should

    be freezed to become immutable https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.concur rent/freeze.html
  15. Why not to use GlobalScope 24 Default background work? Not

    recommended Potential leaks and losing of callbacks ^ l Fix
  16. Sharing data and variables 37 Everything to share should be

    freezed InvalidMutabilityException: mutation attempt of frozen
  17. Flow in Kotlin Common 56 Should be called in coroutines

    scope Even remove of flowOn doesn’t solve problem
  18. Flow in Kotlin Common 57 Flows should be used directly

    in common part Or we need to make a wrapper on iOS side
  19. Summary. 59 • It hard to work with concurrency in

    KMM, but you could work with id. • KMM still be improved • Kotlin/Native still be improved –> wait for issue 462 to be solved
  20. Sources 60 • https://betterprogramming.pub/using-kotlin-flow-in-swift-3e7b53f559b6 • https://github.com/JetBrains/kotlin-native • https://github.com/JetBrains/kotlin-native/blob/master/IMMUTABILITY.md • https://github.com/Kotlin/kotlinx.coroutines/issues/462

    • https://github.com/anioutkazharkova/kmm-concurrency-helper • https://kmpdocs.suparnatural.com/concurrency/#worker • https://elizarov.medium.com/the-reason-to-avoid-globalscope-835337445abc