I'm following <the recommended Codelab> for learni...
# android
d
I'm following the recommended Codelab for learning how to use Room, and they suggest copy and pasting this code.
Copy code
// Annotates class to be a Room Database with a table (entity) of the Word class
@Database(entities = arrayOf(Word::class), version = 1, exportSchema = false)
public abstract class WordRoomDatabase : RoomDatabase() {

   abstract fun wordDao(): WordDao

   companion object {
        // Singleton prevents multiple instances of database opening at the
        // same time. 
        @Volatile
        private var INSTANCE: WordRoomDatabase? = null

        fun getDatabase(context: Context): WordRoomDatabase {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                        context.applicationContext,
                        WordRoomDatabase::class.java, 
                        "word_database"
                    ).build()
                INSTANCE = instance
                return instance
            }
        }
   }
}
The code contains a number of things I don't understand (
@Volatile
,
synchronized
), and Android Studio warns that
synchronized
is a Kotlin internal with no stability guarantees. Copying in something I don't understand which uses internal APIs I don't understand doesn't feel like a great idea. Is there an alternative?
k
d
That documentation states that if your app runs in a single process you should use the singleton design pattern, which I think is exactly what I was asking about