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

SELECT * FROM Kotlin - Droidcon NYC 2019

SELECT * FROM Kotlin - Droidcon NYC 2019

Now that Kotlin is the preferred language of Android developers, what is the best library to work with SQLite databases using Kotlin? Room just released support for Kotlin Coroutines, and SqlDelight released support for Kotlin Multi-platform. Room has great documentation and hides a lot of implementation detail, while SqlDelight lets you write raw SQL statements with an Android Studio plugin and then generates type-safe code for you. In this session, you'll see a rundown of these two libraries, along with live coding demos to help you decide which library sounds right for you in this Kotlin world.

Sam Edwards

August 27, 2019
Tweet

More Decks by Sam Edwards

Other Decks in Programming

Transcript

  1. @ H A N D S TA N D S

    A M # D C N Y C 1 9 WARNING: These slides were not meant to be standalone, but I wanted to share them. This talk heavily switches between Android Studio, Emulator and Chrome. Every time you see it means that I switched over and showed something else. The files are in https://github.com/handstandsam/ShoppingApp. The video will be better to watch when it is out, but it’s not published at this time. E X P O RT E D S L I D E S WA R N I N G Filename.kt
  2. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Why Are We Talking About This Today? ! Kotlin for over 2 years and LOVE it ! Needed to evaluate databases ORMs ! Submitted this talk… and it got accepted ! So… I went a little overboard and decided to dive in heads first
  3. !

  4. !

  5. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Coding Brings Confidence
  6. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Sharing Brings Confidence
  7. @ H A N D S TA N D S

    A M # D C N Y C 1 9 B E G I N N E R E X P E RT
  8. @ H A N D S TA N D S

    A M # D C N Y C 1 9 B E G I N N E R E X P E RT
  9. @ H A N D S TA N D S

    A M # D C N Y C 1 9 B E G I N N E R E X P E RT
  10. @ H A N D S TA N D S

    A M # D C N Y C 1 9 B E G I N N E R E X P E RT
  11. @ H A N D S TA N D S

    A M # D C N Y C 1 9 B E G I N N E R E X P E RT
  12. @ H A N D S TA N D S

    A M # D C N Y C 1 9 B E G I N N E R E X P E RT
  13. !

  14. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Shopping App on GitHub github.com/handstandsam/ShoppingApp
  15. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Shopping App on GitHub github.com/handstandsam/ShoppingApp Shopping App!
  16. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Our Task at Hand ! Make our Shopping Cart functionality persist between sessions as it should increase sales $$$ ! Our codebase has no database currently
  17. # D C N Y C 1 9 @ H

    A N D S TA N D S A M In-Memory Version ! Use an In-Memory Version (No database, but can test functionality) ! Create Interfaces to allow for swapping of DB implementations ! Create a reactive stream that be subscribed to for updates
  18. # D C N Y C 1 9 @ H

    A N D S TA N D S A M In-Memory Version ! Use an In-Memory Version (No database, but can test functionality) ! Create Interfaces to allow for swapping of DB implementations ! Create a reactive stream that be subscribed to for updates ShoppingCartDao.kt & InMemoryShoppingCartDao.kt
  19. # D C N Y C 1 9 @ H

    A N D S TA N D S A M What Library Should We Use? Room SQLDelight
  20. # D C N Y C 1 9 @ H

    A N D S TA N D S A M What Library Should We Use? Room SQLDelight Do We NEED a Local Database?
  21. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Do We NEED a Local Database? ! Shared Preferences? ! Local JSON File ! On the Server?
  22. # D C N Y C 1 9 @ H

    A N D S TA N D S A M With Great Power Comes Great Responsibility ! You are responsible for every byte of data you persist ! Any writes you make will need to be supported during any upgrade ! Migrations can be used to modify data structure
  23. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Reactive Stream for UI Updates ! Shopping Cart reflect the most recent changes to the data. ! Options ! Register a Listener or a callback to our datastore ! RxJava - Flowable ! Android Architecture Components - LiveData ! Kotlin Coroutines - Channels or Flow
  24. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Reactive Stream for UI Updates ! Shopping Cart reflect the most recent changes to the data. ! Options ! Register a Listener or a callback to our datastore ! RxJava - Flowable ! Android Architecture Components - LiveData ! Kotlin Coroutines - Channels or Flow ShoppingCart.kt
  25. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Unit Tests ! Validate our current implementation ! Give confidence in our code
  26. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Unit Tests ! Validate our current implementation ! Give confidence in our code ShoppingCartInMemoryTest.kt
  27. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Our Technical Requirements ! Use a stable, proven version of a library ! Don’t break incremental builds ! Publish database changes reactively ! Make sure you separate code in modules: ! shopping-cart ! shopping-cart-sqldelight ! shopping-cart-room
  28. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room SQLDelight Evaluating Database Libraries
  29. # D C N Y C 1 9 @ H

    A N D S TA N D S A M ! Amazing online documentation ! Proven and written by Google ! Code Generation via Annotation Processing (kapt) breaks incremental builds. (Compiler option, off by default until stable) ! It’s last stable release was June 13 with 2.1.0, but 2.2.0-beta01 just came out on August 22 Room (JetPack Library)
  30. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room ! Setup ! Gradle implementation Dependencies (For Runtime Logic) ! Gradle kapt Dependencies (For Annotation Processing)
  31. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room ! Setup ! Gradle implementation Dependencies (For Runtime Logic) ! Gradle kapt Dependencies (For Annotation Processing) build.gradle
  32. # D C N Y C 1 9 @ H

    A N D S TA N D S A M ! Needed: Entity, Dao, Database Written Code for Room RoomItemInCartEntity RoomItemInCartDao RoomItemInCartDatabase
  33. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room Generated Code RoomItemInCartDao_Impl RoomItemInCartDatabase_Impl
  34. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Generated Code from Annotations is ! Annotations @Entity, @Dao, @Query(“…”) ! Deterministic generated code ! Can break incremental builds
  35. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Using Room in Shopping App SessionGraph.kt
  36. # D C N Y C 1 9 @ H

    A N D S TA N D S A M I ❤ Debugging Features
  37. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Android Debug Database
  38. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Android Debug Database ! https://github.com/amitshekhariitbhu/Android-Debug-Database ! debugImplementation 'com.amitshekhar.android:debug-db:1.0.6' ! Custom Port Config, forwarded from Emulator: ! debug { resValue("string", "PORT_NUMBER", “8081") } ! adb forward tcp:8081 tcp:8081 ! http://localhost:8081 ! Image: https://www.todayifoundout.com/wp-content/uploads/2017/11/rick- astley.png
  39. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Testing Room ! Unit test must run on an Android device ! https://developer.android.com/training/data-storage/room/testing-db ! Can be run without UI ! ./gradlew shopping-cart-room:connectedAndroidTest --parallel
  40. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Testing Room ! Unit test must run on an Android device ! https://developer.android.com/training/data-storage/room/testing-db ! Can be run without UI, so runs fast ! ./gradlew shopping-cart-room:connectedAndroidTest --parallel RoomShoppingCartTest.kt
  41. # D C N Y C 1 9 @ H

    A N D S TA N D S A M ! Generates Kotlin code as of v1.0.0 ! Is a stable release ! Does incremental code generation from SQL files, without using annotations (kapt) ! Supports Kotlin Multiplatform SQLDelight (Written by Square)
  42. # D C N Y C 1 9 @ H

    A N D S TA N D S A M SQLDelight ! Setup ! Gradle Plugin (To Perform Code Generation) ! Gradle implementation Dependencies (For Runtime Logic)
  43. # D C N Y C 1 9 @ H

    A N D S TA N D S A M SQLDelight ! Setup ! Gradle Plugin (To Perform Code Generation) ! Gradle implementation Dependencies (For Runtime Logic) build.gradle
  44. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Written SQL for SQLDelight ItemInCartEntity.sq
  45. # D C N Y C 1 9 @ H

    A N D S TA N D S A M SQLDelight Code Generation ! Leverages Gradle Plugin ! Detects changes in the .sq files and will regenerate if needed ! Provides incremental builds
  46. # D C N Y C 1 9 @ H

    A N D S TA N D S A M ItemInCart.kt ItemInCartEntityQueries.kt
 DatabaseImpl.kt Generated Code for SQLDelight
  47. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Running SQLDelight SessionGraph.kt
  48. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Showing SQLDelight Works Android Debug Database
  49. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Unit Testing ! JVM Driver allowing it to run on the JVM without any Android dependencies. ! Fast TDD development ! ./gradlew shopping-cart-sqldelight:testDebug
  50. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Unit Testing ! JVM Driver allowing it to run on the JVM without any Android dependencies. ! Fast TDD development ! ./gradlew shopping-cart-sqldelight:testDebug SqlDelightShoppingCartTestDelegate.kt
  51. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Kotlin Multiplatform ! https://github.com/square/sqldelight/tree/master/sample ! Android ! iOS
  52. # D C N Y C 1 9 @ H

    A N D S TA N D S A M AndroidX SupportSQLiteDatabase ! “A database abstraction which removes the framework dependency and allows swapping underlying sql versions. It mimics the behavior of SQLiteDatabase” ! https://developer.android.com/reference/androidx/sqlite/db/ SupportSQLiteDatabase
  53. @ H A N D S TA N D S

    A M # D C N Y C 1 9
  54. @ H A N D S TA N D S

    A M # D C N Y C 1 9
  55. @ H A N D S TA N D S

    A M # D C N Y C 1 9 SQLDelight Room
  56. @ H A N D S TA N D S

    A M # D C N Y C 1 9 SQLDelight Room
  57. @ H A N D S TA N D S

    A M # D C N Y C 1 9 SQLDelight
  58. # D C N Y C 1 9 @ H

    A N D S TA N D S A M https://handstandsam.com/2019/03/10/it-depends-is-the-answer-to-your-android-question/
  59. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests
  60. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests
  61. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests
  62. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests
  63. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests
  64. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests
  65. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests
  66. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests
  67. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests