I'm following the tutorial on dependency injection with Hilt
using the Google Sunflower app repo:
@InstallIn(SingletonComponent::class)
@Module
class DatabaseModule {
@Singleton
@Provides
fun provideAppDatabase(@ApplicationContext context: Context): AppDatabase {
return AppDatabase.getInstance(context)
}
@Provides
fun providePlantDao(appDatabase: AppDatabase): PlantDao {
return appDatabase.plantDao()
}
@Provides
fun provideGardenPlantingDao(appDatabase: AppDatabase): GardenPlantingDao {
return appDatabase.gardenPlantingDao()
}
}
I noticed they did not use the Singleton
annotation for the two functions below provideAppDatabase()
.
Does this mean that by using the Singleton
annotation on provideAppDatabase
, automatically make the two functions providePlantDao()
and provideGardenPlantingDao()
Singletons?
Other tutorials I've checked, annotate the Dao
provide functions with @Singleton
.
@Module
@InstallIn(SingletonComponent::class)
object DbModule {
@Provides
@Singleton
fun provide(@ApplicationContext context: Context) = Room.databaseBuilder(
context, NoteDatabase::class.java, NOTE_DATABASE)
.allowMainThreadQueries()
.fallbackToDestructiveMigration()
.build()
@Provides
@Singleton
fun provideDao(db: NoteDatabase) = db.noteDoa()
}
https://androidgeek.co/how-to-use-hilt-with-room-database-complete-guide-part-2-2c2bbf52f610
So I'm confused on to annotate or not to.
from Hilt - Question on making a Dao object in a module a Singleton
No comments:
Post a Comment