Hello again, folks. I'm running a study project in...
# koin
p
Hello again, folks. I'm running a study project in Kotzilla Platform. It's a fresh created project. And I'm very curious in how I can speed up my
ViewModel
intialization. I've shared my
ViewModel
constructor above. And now I'm sharing the other dependencies. GetUsers.kt
Copy code
@Factory
class GetUsers(
    private val dao: UserDao
)
AppModule.kt
Copy code
@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
Copy code
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?
k
@Pedro Francisco de Sousa Neto do you notice any improvements when you remove
allowMainThreadQueries
? I think @amal could answer your questions better
p
Hey, I've did some changes.
First of all, I've updated my Koin Dependency Injection (Plugin) to version
1.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!
🎉 4
Then, I've removed the
allowMainThreadQueries()
. And refactored the `AppModule`:
Copy code
@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()
    }
}
🙌 3
So I ran the app, checked it on the Kotzilla Platform, and voilà! An optimization of 40,28% (1,44ms)!
❤️ 5
a
Great work, @Pedro Francisco de Sousa Neto!