Rather than converting repository classes to Singletons, they should be objects.
Totally agree. Just wonder why still need these things on Kotlin
companion object {
// For Singleton instantiation
@Volatile private var instance: GardenPlantingRepository? = null
fun getInstance(gardenPlantingDao: GardenPlantingDao) =
instance ?: synchronized(this) {
instance ?: GardenPlantingRepository(gardenPlantingDao).also { instance = it }
}
}
I had the same question before, however, after some sleeping and thinking I find that the object class hasn't chance to consume parameters, I think this is the only reason to use this style, when there isn't DI plugin in the project.
This is the kind of code that makes your average developer's head spin and is what has been driving complaints about Android development being hard to get right. Really jetpack project goal is to make it easier to write android code and not force developers to write or understand these kinds of things. If sunflower project contains code like this then clearly jetpack project isn't really complete!
Double checked locking is a cool performance trick but it should be an internal implementation detail that your average dev need not understand or write by hand over and over again.