Room의 좋은 점
- SQL 쿼리의 컴파일 시간 확인
- 반복적이고 오류가 발생하기 쉬운 상용구 코드를 최소화하는 편의 주석
- 간소화된 데이터베이스 이전 경로
Room에는 다음 3가지 주요 구성 요소가 있다.
- Database class : 데이터베이스를 보유하고 앱의 영구 데이터와의 기본 연결을 위한 엑세스 포인트 역할을 한다.
- Entity : 앱 데이터베이스의 테이블을 나타냅니다.
- Data Access Objects(DAO) : 앱이 데이터베이스의 데이터를 쿼리, 업데이트, 삽입, 삭제하는 데 사용할 수 있는 메서드를 제공합니다.
Database
데이터베이스를 보유할 AppDatabase
클래스를 정의한다.
AppDatabase
는 데이터베이스 구성을 정의하고 영구 데이터에 대한 앱의 기본 액세스 포인트 역할을 한다. Database class는 다음 조건을 충족한다.
entities
배열이 보함된 @Database
주석을 달아야 한다.RoomDatabase
를 확장하는 추상 클래스여야 한다.@Database(entities = [User::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}
사용
val db = Room.databaseBuilder( applicationContext, AppDatabase::class.java, "database-name" ).build()
AppDatabase
의 추상 메서드를 사용하여 DAO 인스턴스를 가져올 수 있다. 결과적으로 DAO 인스턴스의 메서드를 사용하여 데이터베이스와 상호작용 할 수 있다. val userDao = db.userDao() val users: List<User> = userDao.getAll()
plugins { id 'kotlin-kapt' } dependencies { // ROOM Database 사용 implementation("androidx.room:room-runtime:2.4.3") annotationProcessor("androidx.room:room-compiler:2.4.3") kapt("androidx.room:room-compiler:2.4.3") implementation("androidx.room:room-ktx:2.4.3") }
@Database(entities = [WordData::class], version = 1) abstract class AppDatabase : RoomDatabase() { abstract fun wordDao() : WordDao companion object{ private var INSTANCE : AppDatabase? = null fun getInstance(context : Context):AppDatabase? { if(INSTANCE == null) { synchronized(AppDatabase::class.java){ INSTANCE = Room.databaseBuilder( context.applicationContext, AppDatabase::class.java, "app-database.db" ).build() } } return INSTANCE } } }
@Parcelize @Entity(tableName = "word") data class WordData( val word : String, val mean : String, val type : String, @PrimaryKey(autoGenerate = true) val id: Int = 0, ) : Parcelable
@Dao interface WordDao { @Query("SELECT * from word ORDER BY id DESC") fun getAll() : List<WordData> @Query("SELECT * from word ORDER BY id DESC LIMIT 1") fun getLatestWord() : WordData @Insert fun insert(word : WordData) @Delete fun delete(word : WordData) @Update fun update(word : WordData) }
val list = AppDatabase.getInstance(this)?.wordDao()?.getAll() ?: emptyList()
wordAdapter.itemList.addAll(list)
AppDatabase.getInstance(this)?.wordDao()?.getLatestWord()?.let {word ->
wordAdapter.itemList.add(0, word)
runOnUiThread { wordAdapter.notifyDataSetChanged() }
}
AppDatabase.getInstance(this)?.wordDao()?.insert(item)
AppDatabase.getInstance(this)?.wordDao()?.update(word)