So, in previous post (Minimal Kotlin Multiplatform project using Compose and SwiftUI) I gave brief overview of PeopleInSpace, a small repository that was designed to illustrate key moving parts of a Kotlin Multiplatform project (running on Android, iOS, and later, watchOS). However, although I’m reluctant to add much else to the project, one key gap was around locally persisting the data we got back from remote server. The somewhat serendipitous combination of following activities in last few weeks also provided further encouragement to add such functionality to the project (1) Neal’s addition of watchOS support to PeopleInSpace, and (2) watchOS support was added to SQLDelight (Kotlin Multiplatform persistence library).
SQLDelight 1.2.2 released with windows/watchOS/tvOS/macOS support!https://t.co/vK27TSSVqF
— Alec Strong (@Strongolopolis) January 23, 2020
also the whole thing is on github actions now! There's even an EAP channel for the intellij plugin that automatically gets pushed too!!! Wow!!!!!
I’ve described usage of SQLDelight in a previous post (Introduction to Multiplatform Persistence with SQLDelight) and this duplicates some of the information in there but I felt it was still useful to see this again in slightly different context…and also, importantly, we can now take advantage of upcoming Kotlin Native multi-threaded coroutines support (along with SQLDelight’s Flow support)
Adding SQLDelight support
So, changes to add SQLDelight to project were made in following commit.
We start by adding use of com.squareup.sqldelight
plugin to shared module build.gradle
along with associated sqldelight
configuration.
We also added dependency to com.squareup.sqldelight
libraries for various platforms - note use since 1.2.2 release of com.squareup.sqldelight:native-driver
for
native (e.g. iOS/watchOS) targets.
This is combined with PeopleInSpace.sq
to automatically generate persistence code needed (in particular PeopleInSpaceDatabase
and PeopleInSpaceQueries
)
We can now make use of generated code mentioned above to add persistence functionality to PeopleInSpaceRepository
(Note use of SQLDelight’s
asFlow()
method). We use expect/actual to allow provision of versions of createDb()
specific to each platform (see repository for details).
Note there’s currently an issue running this in Watch Simulator (see this tweet for more details….will post update here when that’s resolved).
Featured in Kotlin Weekly Issue #183
Related tweet
Wrote short article about addition of SQLDelight to PeopleInSpace #KotlinMultiplatform sample https://t.co/U6mHKncOIq
— John O'Reilly (@joreilly) January 25, 2020
This makes use of @Strongolopolis's addition of watchOS support to library (building on @thorinside's recent addition of watchOS support to PeopleInSpace repo) https://t.co/J51MS2yDp5