I am trying to create singleton room database. I found 2 solutions but I don't know what's difference between them.
According to this document https://developer.android.com/codelabs/kotlin-android-training-room-database?hl=en&continue=https%3A%2F%2Fcodelabs.developers.google.com%2F%3Fcat%3Dandroid#5
companion object {
@Volatile private var INSTANCE:AppDatabase? = null
fun getInstance(context: Context):AppDatabase {
synchronized(this){
var instance = INSTANCE
if (instance == null){
instance = Room.databaseBuilder(context.applicationContext,AppDatabase::class.java,"user_table")
.fallbackToDestructiveMigration()
.build()
INSTANCE = instance
}
return instance
}
}
And this one according to Singleton class in Kotlin
companion object {
fun getInstance(context: Context):AppDatabase{
return Room.databaseBuilder(context.applicationContext,AppDatabase::class.java,"user_table")
.fallbackToDestructiveMigration()
.build()
}
}
I tried these and they all give me same instance. Is there any difference between them? In terms of performance or whatever