Pedro Francisco de Sousa Neto
07/23/2025, 6:15 PMViewModel
intialization.
I've shared my ViewModel
constructor above. And now I'm sharing the other dependencies.
GetUsers.kt
@Factory
class GetUsers(
private val dao: UserDao
)
AppModule.kt
@Module
@ComponentScan("br.com.velantasistemas.mymoney")
class AppModule {
@Single
fun getAppDatabase(): AppDatabase {
return get(AppDatabase::class.java)
}
@Single
fun getUserDao(): UserDao {
return getAppDatabase().userDao()
}
}
DatabaseModule.kt
object DatabaseModule {
val module = module {
single<AppDatabase> {
val context: Context = get()
return@single Room.databaseBuilder(
context,
AppDatabase::class.java, "database-name"
)
.allowMainThreadQueries()
.fallbackToDestructiveMigration(dropAllTables = true)
.build()
}
}
}
Questions:
1. How can I speed this up?
2. Could allowMainThreadQueries
be the reason for this slowdown?
3. Do you think this is a good way to handle database and DAO access?
a. I know it's a matter of preference, but I'd really like to hear your thoughts, folks.
4. I remember using Koin's suspend initialization before.
a. Does it help only during app startup, or could it also be beneficial for initializing singletons like the database in this case?Kibet Theophilus
07/31/2025, 3:30 PMallowMainThreadQueries
?
I think @amal could answer your questions betterPedro Francisco de Sousa Neto
08/02/2025, 1:13 AMPedro Francisco de Sousa Neto
08/02/2025, 1:14 AM1.3.1
- in Android Studio. And I was able to discover something really interesting in my project! (CC @Olwethu PISSOT)
The plugin detected the DatabaseModule.module
as not used. So I've moved the Room initialization to the getAppDatabase()
. And deleted the DatabaseModule
file.
Very nice feature, folks!Pedro Francisco de Sousa Neto
08/02/2025, 1:16 AMallowMainThreadQueries()
. And refactored the `AppModule`:
@Module
@ComponentScan("br.com.velantasistemas.mymoney")
class AppModule {
@Single
fun getAppDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(
context = context,
klass = AppDatabase::class.java, name = "database-name"
)
.fallbackToDestructiveMigration(dropAllTables = true)
.build()
}
@Single
fun getUserDao(context: Context): UserDao {
return getAppDatabase(context).userDao()
}
}
Pedro Francisco de Sousa Neto
08/02/2025, 1:19 AMamal
08/04/2025, 11:11 AM