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.

C6e1201f51c1ff186edba69f98476f15?s=128

Dinorah Tovar

October 30, 2020
Tweet

Transcript

  1. Kotlin Multiplatform for iOS Developers Dinorah Tovar Platform Mobile Engineer

    @ konfío.mx @ddinorahtovar @ddinorahtovar
  2. Kotlin and Swift are like cousins

  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” }
  4. Kotlin is not for Android only

  5. Kotlin is for multiple things!

  6. Kotlin is for Multiple things @ddinorahtovar Mobile Cross platform Server

    Side Android Native Web Development Data Science
  7. KMP is for Business Logic @ddinorahtovar KMP Business Logic Models

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

    Multiplatform Kotlin Compiler Native JS JVM Binaries for iOS Or Windows
  9. Kotlin is for Multiple things @ddinorahtovar Native Binaries for iOS

    Relies on Objective C Objective-C headers that are annotated for Swift
  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
  11. What to expect from KMP @ddinorahtovar Kotlin Multiplatform First Gen

    Support Android and iOS SDK
  12. Sharing the logic! @ddinorahtovar expect fun / class actual fun

    / class declaration Import iOS libraries actual fun / class declaration Import JVM libraries
  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) } }
  14. Sharing the logic! @ddinorahtovar Ktor / Kotlin Serialization For IO

    Apollo For GraphQL SQLDelight Kodein SQLiter For Databases Kotlinx-dateTime For Dates
  15. Kotlin has Coroutines

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

  17. Coroutines @ddinorahtovar UI Thread Coroutine Thread Dispatcher IO

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

    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
  19. Databases

  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(?,?);
  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 ) } } }
  22. Sharing Databases @ddinorahtovar expect fun createDb() : ProductDB actual fun

    createDb(): MyDatabase { val products = NativeSqliteDriver(MyDatabase.Schema, “products.db”) return MyDatabase(driver) }
  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) }
  24. Give it a try!

  25. Kotlin Multiplatform for iOS Developers Dinorah Tovar Platform Mobile Engineer

    @ konfío.mx @ddinorahtovar @ddinorahtovar