https://kotlinlang.org logo
#squarelibraries
Title
# squarelibraries
c

Colton Idle

07/29/2023, 3:31 PM
some interesting bits: 1. i thought i needed to find a separate dependency for jdbc since it wasn't importing in my test. didn't know that jdbc came out of the box. lol 2. turns out #1 was caused by a java version mismatch that i didnt find until i tried to actually build everything 3. my tests were initially failing because i didn't call
Database.Schema.create(driver)
which was just because my android code didn't have that. wonder why i need that on the jvm but not android? 4. in android land i know that its typically good to only keep one database object around as a singleton that DI'd everywhere. in the case of SQLDelight, I'm not sure what I should keep as singletons. There's the
Driver
,
Database
and
*Queries
. any tips on that?
for example. this is how my persistenceService looks
its just for poc purposes right now, but im wondering if the database could be pulled out as a singleton (like the driver) and maybe the queries too?
k

kevin.cianfarini

07/29/2023, 3:42 PM
I am fairly certain, but am happy to be corrected, that you want to keep a single db instance around or else you run the risk of listeners not operating properly with sqlite
Also I normally just inject an instance of
Database
into my classes and not the query classes themselves.
c

curioustechizen

07/29/2023, 3:49 PM
I vaguely remember this being called out in the documentation: that you should be keeping a single instance of the Database.
👍 1
j

Javier

07/29/2023, 4:39 PM
you can inject the database as a singleton with Koin or Hilt and pick it in whatever implementation detail For example
and with Koin:
Copy code
private val module = module {
    single<SqlDriver> { AndroidSqliteDriver(PokemonDatabase.Schema, get(), "pokemon.db") }
    single<PokemonDatabase> { PokemonDatabaseFactory(get(), get()).create() }
    ...
}