$30 off During Our Annual Pro Sale. View Details »

Kotlin Multiplatform for iOS Developers (Lightning talk)

Kotlin Multiplatform for iOS Developers (Lightning talk)

In this talk, we will check some tricks inside the incredible world of Kotlin Multiplatform for iOS. Kotlin Multiplatform Mobile (KMM) is an SDK that allows you to use the same business logic code in both iOS and Android applications.

Dinorah Tovar

October 30, 2020
Tweet

More Decks by Dinorah Tovar

Other Decks in Technology

Transcript

  1. Kotlin Multiplatform
    for iOS Developers
    Dinorah Tovar
    Platform Mobile Engineer
    @ konfío.mx
    @ddinorahtovar
    @ddinorahtovar

    View Slide

  2. Kotlin and Swift are
    like cousins

    View Slide

  3. Like cousins?
    @ddinorahtovar
    public var description: String {

    switch self {

    case .active:

    return "active"

    case .offer:

    return "offer"

    case .uploadDocuments:

    return "documents"

    case .waitingDisbursement:

    return "waitingDisbursement"

    case .former:

    return "former"

    }

    }
    fun description(it: String) =

    when (this) {

    active #-> "active"

    offer #-> "offer"
    uploadDocuments #-> “documents"

    waitingDisbursement #->
    “waitingDisbursement"

    former #-> “former”

    }

    View Slide

  4. Kotlin is not for Android
    only

    View Slide

  5. Kotlin is for multiple
    things!

    View Slide

  6. Kotlin is for Multiple things
    @ddinorahtovar
    Mobile Cross
    platform
    Server Side Android
    Native Web Development Data Science

    View Slide

  7. KMP is for Business Logic
    @ddinorahtovar
    KMP
    Business Logic Models

    View Slide

  8. Kotlin is for Multiple things
    @ddinorahtovar
    JAR/AAR (For Android)
    Kotlin
    Multiplatform
    Kotlin Compiler

    Native
    JS
    JVM

    Binaries for iOS
    Or Windows

    View Slide

  9. Kotlin is for Multiple things
    @ddinorahtovar
    Native

    Binaries for iOS
    Relies on Objective C

    Objective-C headers that are annotated for Swift

    View Slide

  10. What to expect from KMP
    @ddinorahtovar
    Name Folder Target Result
    Common
    iOS
    Android
    SharedCode/commonMain/kotlin
    SharedCode/iosMain
    SharedCode/androidMain/kotlin
    -
    iOS arm 64
    or
    x86_64
    JVM 1.6
    Kotlin metadata
    Apple framework
    .jar file or
    .class files

    View Slide

  11. What to expect from KMP
    @ddinorahtovar
    Kotlin Multiplatform
    First Gen Support
    Android and iOS
    SDK

    View Slide

  12. Sharing the logic!
    @ddinorahtovar
    expect fun / class

    actual fun / class declaration

    Import iOS libraries

    actual fun / class declaration

    Import JVM libraries

    View Slide

  13. Sharing the logic!
    @ddinorahtovar
    expect fun saveValue(value: String)

    actual fun saveValueLocally(value: String) {

    NSUserDefaults.standardUserDefaults.setValue(

    value,

    forKey = "Value"

    )

    }
    actual fun saveValueLocally(value: String) {

    sharedPreferences.edit {
    putString("Value", value)
    }

    }

    View Slide

  14. Sharing the logic!
    @ddinorahtovar
    Ktor / Kotlin
    Serialization
    For IO
    Apollo
    For GraphQL
    SQLDelight
    Kodein
    SQLiter
    For Databases
    Kotlinx-dateTime
    For Dates

    View Slide

  15. Kotlin has Coroutines

    View Slide

  16. Coroutines
    @ddinorahtovar
    •Suspended computation
    •Coroutines support!
    •Cold & Hot Streams

    View Slide

  17. Coroutines
    @ddinorahtovar
    UI Thread
    Coroutine Thread
    Dispatcher IO

    View Slide

  18. Coroutines
    @ddinorahtovar
    suspend fun getProducts() {}
    fun getProducts(callback: List #-> Unit) {

    GlobalScope.launch(ApplicationDispatcher) {

    callback(model.getProducts())

    }

    }
    getProducts{ (products: [Product]) #-> () in

    #// Do something here with product

    }

    iOS Call from Controller
    Suspend function in Kotlin Common
    suspend fun getProducts() {}
    Coroutine + Dispatcher

    View Slide

  19. Databases

    View Slide

  20. Sharing Databases
    @ddinorahtovar
    CREATE TABLE ProductList(

    product_id INTEGER NOT NULL PRIMARY KEY,

    product_name TEXT NOT NULL

    );

    insertItem:

    INSERT OR REPLACE INTO ProductList(

    product_id,

    product_name

    )VALUES(?,?);

    View Slide

  21. Sharing Databases
    @ddinorahtovar
    expect fun createDb() : ProductDB
    class ProductsRepository {

    private val productApi = ProductApi()

    private val productDB = createDb()

    private val productQueries = productDB.queries
    suspend fun fetchProducts() {

    val productsAvailable = productApi.fetchProducts()

    productsAvailable.forEach {

    productQueries.insertItem(

    it.product_id.toLong(),

    it.product_name

    )

    }

    }

    }

    View Slide

  22. Sharing Databases
    @ddinorahtovar
    expect fun createDb() : ProductDB

    actual fun createDb(): MyDatabase {

    val products = NativeSqliteDriver(MyDatabase.Schema, “products.db”)

    return MyDatabase(driver)

    }

    View Slide

  23. Sharing Databases
    @ddinorahtovar
    let productRepository = ProductRepository()

    productRepository.fetchProduct(success: { data in

    self.listProduct = data

    self.tableView.reloadData()

    return KotlinUnit()

    })

    actual fun createDb(): MyDatabase {

    val products = NativeSqliteDriver(MyDatabase.Schema, “products.db”)

    return MyDatabase(driver)

    }

    View Slide

  24. Give it a try!

    View Slide

  25. Kotlin Multiplatform
    for iOS Developers
    Dinorah Tovar
    Platform Mobile Engineer
    @ konfío.mx
    @ddinorahtovar
    @ddinorahtovar

    View Slide