Skip to content

Commit

Permalink
create databse with callback
Browse files Browse the repository at this point in the history
  • Loading branch information
kulloveth committed Apr 26, 2020
1 parent 27642ce commit 64ac361
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.developer.kulloveth.expandablelistsamplewithroom.data.model

class Repository {

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ interface ContinentDao {
fun getAlphabetizedWords(): LiveData<List<Continents>>

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(continent: Continents)
fun insert(continent: Continents)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ interface CountryDao {
fun getAlphabetizedWords(): LiveData<List<Countrys>>

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(country: Countrys)
fun insert(country: Countrys)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.developer.kulloveth.expandablelistsamplewithroom.data.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.developer.kulloveth.expandablelistsamplewithroom.data.model.Continents
import com.developer.kulloveth.expandablelistsamplewithroom.data.model.Countrys
import java.util.concurrent.Executors

@Database(entities = arrayOf(Continents::class, Countrys::class), version = 1, exportSchema = false)
public abstract class PlaceDatabase : RoomDatabase() {

abstract fun continentDao(): ContinentDao
abstract fun countryDao(): CountryDao
private var mContext: Context? = null

companion object {
@Volatile
private var INSTANCE: PlaceDatabase? = null

fun getDatabase(context: Context): PlaceDatabase {

return INSTANCE ?: synchronized(this) {
INSTANCE ?: buildDatabase(context).also {
INSTANCE = it
}
}
}

private fun buildDatabase(context: Context): PlaceDatabase {
return Room.databaseBuilder(context, PlaceDatabase::class.java, "place_db")
.addCallback(object : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)

Executors.newSingleThreadExecutor().execute {
INSTANCE?.let {

// it.continentDao().insert()
}
}

}
}).build()


}
}

}

0 comments on commit 64ac361

Please sign in to comment.