Skip to content

Latest commit

ย 

History

History
119 lines (91 loc) ยท 4.83 KB

room.md

File metadata and controls

119 lines (91 loc) ยท 4.83 KB

Room

Room ์ด๋ž€ ?

Room์€ ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ์—์„œ SQLite ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‰ฝ๊ณ  ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.
Entity, DAO, Room Database ์ด ์„ธ ๊ฐœ์˜ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ํ†ตํ•ด Room ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๊ตฌ์„ฑํ•œ๋‹ค.

๊ตฌ์„ฑ์š”์†Œ

  • Entity : Room์œผ๋กœ ์ž‘์—…ํ•  ๋•Œ ํ…Œ์ด๋ธ”์„ ์„ค๋ช…ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด ๋‹ฌ๋ฆฐ ํด๋ž˜์Šค
  • RoomDataBase : SQLite ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•œ ์•ก์„ธ์Šค ํฌ์ธํŠธ ์—ญํ• , ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ „์ฒด์ ์ธ ์†Œ์œ ์ž ์—ญํ• ์„ ํ•˜๋ฉฐ DB๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ๋ฒ„์ „์„ ๊ด€๋ฆฌ
  • DAOData Access Object : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜์—ฌ ์ˆ˜ํ–‰ํ•  ์ž‘์—…์„ ๋ฉ”์†Œ๋“œ ํ˜•ํƒœ๋กœ ์ •์˜, SQL ์ฟผ๋ฆฌ๋ฅผ ํ•จ์ˆ˜์— ๋งคํ•‘ํ•œ๋‹ค. (select, insert, delete, join...) image

Room vs SQLite

Room SQLite
์ปดํŒŒ์ผํƒ€์ž„์— ์ฟผ๋ฆฌ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ โœ… โŒ
Schema๊ฐ€ ๋ณ€๊ฒฝ๋  ๊ฒฝ์šฐ SQL ์ฟผ๋ฆฌ ์—…๋ฐ์ดํŠธ โœ…(์ž๋™์—…๋ฐ์ดํŠธ) โœ…(์ˆ˜๋™์—…๋ฐ์ดํŠธ)
๋ณด์ผ๋Ÿฌํ”Œ๋ ˆ์ดํŠธ โŒ(์ƒ์šฉ๊ตฌ ์—†์ด ORM๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด ๋งคํ•‘) โœ…(๋งŽ์€ ์ƒ์šฉ๊ตฌ ํ•„์š”)
LiveData, RxJava, Coroutine ์ง€์› โœ… โŒ

์„ค์ •

dependencies { 
    // Room components
    implementation "androidx.room:room-ktx:$rootProject.roomVersion"
    kapt "androidx.room:room-compiler:$rootProject.roomVersion"
    androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
}

Entity ์ƒ์„ฑ

@Entity(tableName = "word_table")
data class Word(
    @PrimaryKey @ColumnInfo(name = "word") val word: String

)
@PrimaryKey*(autoGenerate = true) val id : Int,

@ColumnInfo(name = "word") val word: String
  • @Entity(tableName = "word_table")
    • @Entity ์–ด๋…ธํ…Œ์ด์…˜์ด ๋‹ฌ๋ฆฐ ํด๋ž˜์Šค๋Š” SQLite ํ…Œ์ด๋ธ”์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
    • ํด๋ž˜์Šค์ด๋ฆ„๊ณผ ๋‹ค๋ฅธ ์ด๋ฆ„์„ ์›ํ•˜๋Š” ๊ฒฝ์šฐ tableName ์†์„ฑ์„ ์ง€์ •ํ•œ๋‹ค
  • @PrimaryKey ๋ชจ๋“  ์—”ํ‹ฐํ‹ฐ์—๋Š” ๊ธฐ๋ณธํ‚ค๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
  • @ColumnInfo(name= "word") ํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ์ด ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„๊ณผ ๋‹ค๋ฅธ ์ด๋ฆ„์„ ์›ํ•œ๋‹ค๋ฉด ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค
  • PrimaryKey(๊ธฐ๋ณธํ‚ค)์— autoGenerate ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ ์œ ํ‚ค๋ฅผ ์ž๋™ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค

DAO

DAO(Data Access Object)์—์„œ SQL ์ฟผ๋ฆฌ๋ฅผ ์ง€์ •ํ•˜๊ณ  ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ๋กœ ์—ฐ๊ฒฐํ•œ๋‹ค.
DAO๋Š” ์ธํ„ฐํŽ˜์ด์Šค ๋˜๋Š” ์ถ”์ƒํด๋ž˜์Šค์—ฌ์•ผ ํ•œ๋‹ค.
๊ธฐ๋ณธ์ ์œผ๋กœ ์ฟผ๋ฆฌ๋Š” ๋ณ„๋„์˜ ์Šค๋ ˆ๋“œ์—์„œ ์‹คํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค.

@Dao
interface WordDao {

    @Query("SELECT * FROM word_table ORDER BY word ASC")
    fun getAlphabetizedWord(): List<Word>

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insert(word: Word)

    @Query("DELETE FROM word_table")
    suspend fun deleteAll()
}
  • @Query("SELECT * FROM word_table ORDER BY word ASC") ์ฟผ๋ฆฌ ์„ค์ •
  • @Insert ์–ด๋…ธํ…Œ์ด์…˜์€ SQL์„ ์ œ๊ณตํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค
  • onConflict = OnConflictStrategy.IGNORE ์ด๋ฏธ ๋ชฉ๋ก์— ์žˆ๋Š” ๋‹จ์–ด์™€ ์ •ํ™•ํžˆ ๋™์ผํ•œ ๊ฒฝ์šฐ ์ƒˆ ๋‹จ์–ด๋ฅผ ๋ฌด์‹œํ•˜๋Š” ์ถฉ๋Œ ์ „๋žต์„ ์„ค์ •ํ•œ๋‹ค

Room DAO ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

Room Database ๊ตฌํ˜„

Room Database ํด๋ž˜์Šค๋Š” ์ถ”์ƒ์ ์ด๊ณ  ํ™•์žฅ๋˜์–ด์•ผํ•œ๋‹ค.
์ผ๋ฐ˜์ ์œผ๋กœ ์ „์ฒด ์•ฑ์—๋Œ€ํ•ด Room Database์˜ ์ธ์Šคํ„ด์Šค๋Š” ํ•˜๋‚˜๋งŒ ํ•„์š”ํ•˜๋‹ค.

@Database(entities = [Word::class], version = 1, exportSchema = false)
abstract class WordRoomDataBase : RoomDatabase() {

    abstract fun wordDao(): WordDao

    companion object {

        @Volatile
        private var INSTANCE: WordRoomDataBase? = null

        fun getDatabase(context: Context): WordRoomDataBase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    WordRoomDataBase::class.java,
                    "word_database"
                ).build()
                INSTANCE = instance
                instance
            }
        }
    }
}
  • WordRoomDataBase ํด๋ž˜์Šค์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ๋˜๋„๋ก @Database ์–ด๋…ธํ…Œ์ด์…˜์„ ์„ค์ •ํ•œ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์†ํ•œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์„ ์–ธํ•˜๊ณ , ๋ฒ„์ „๋ฒˆํ˜ธ๋ฅผ ์„ค์ •ํ•œ๋‹ค
  • ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋นŒ๋“œ ๊ฒฝ๊ณ ๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด exportSchema๋ฅผ false๋กœ ์„ค์ •ํ•œ๋‹ค
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ๊ฐ @Dao์— ๋Œ€ํ•œ ์ถ”์ƒ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด dao๋ฅผ ๋…ธ์ถœํ•œ๋‹ค
  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ธ์Šคํ„ด์Šค๋Š” 1๊ฐœ๋งŒ ํ”ผ์š”ํ•˜๋ฏ€๋กœ ๋‹ค์ค‘์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐฉ์ง€ํ•œ๋‹ค object, synchronized์„ ์‚ฌ์šฉํ•˜์—ฌ ์‹ฑ๊ธ€ํ†ค๊ตฌํ˜„
  • Room.databaseBuilder().build()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค