I want to create multiple, separate databases of the same structure (all using the same RoomDatabase class and DAOs). I'm using Room DB and Hilt, and am using a provider like below to create the database:
@Singleton
@Provides
fun provideMyDatabase(@ApplicationContext context: Context): MyDatabase =
Room
.databaseBuilder(context, MyDatabase::class.java, name = dbName)
.build()
The dbName is not known until runtime, and will be changed during the app. Even if this is possible, this feels like the wrong way to go about this but I'm not sure of any alternative. The number of DBs that will be created is dynamic, but all are of type MyDatabase
(with a different dbName
). Appreciate any thoughts in advance.
So if I'm understanding correctly you just want two files that have the same database schema.
That shouldn't be too hard to achieve.
If you don't have any other dependencies you're not really getting the most bang for your buck with Hilt here.
basic use case
class DbFactory @Inject constructor(
@ApplicationContext context: Context
) {
fun create(dbName: String): MyDatabase = Room
.databaseBuilder(context, MyDatabase::class.java, name = dbName)
.build()
}
With the above just inject the factory and use the same db name each time you want to get it out to read/write the data.
It only really gets fun if you want to inject extra things when creating the db. In which case you could consider something like @AssistedInject
https://dagger.dev/dev-guide/assisted-injection.html
In which case you'd just make a factory with some kind of method to pass in the db name.
Assuming you want some kind of Repository style wrapper around the db to run insert/queries on you could do something along these lines...
@AssistedFactory
interface MyRepositoryLogicFactory {
create(name: String): MyRepositoryLogic
}
class MyRepositoryLogic @AssistedInject constructor(
@ApplicationContext context: Context,
dependencyA: DependencyA,
dependencyB: DependencyB,
@Assisted dbName: String
//add any other injected dependencies here
) {
private val db = Room
.databaseBuilder(context, MyDatabase::class.java, name = dbName)
.build()
//...do whatever with the db in here..
}
same rules apply as the first example. you'd inject the factory into whatever needs rather than the db, and use the same dbName
each time you want to generate an instance to do things against.