126 lines
3.6 KiB
Markdown
126 lines
3.6 KiB
Markdown
---
|
|
id: android-room-patterns
|
|
title: Android Room — 안전한 SQLite ORM
|
|
category: Coding
|
|
status: draft
|
|
source_trust_level: B
|
|
verification_status: conceptual
|
|
created_at: 2026-05-09
|
|
updated_at: 2026-05-09
|
|
tags: [android, room, sqlite, persistence, vibe-coding]
|
|
tech_stack: { language: "Kotlin / Room", applicable_to: ["Android"] }
|
|
applied_in: []
|
|
aliases: [Room, DAO, Entity, Migration, Flow]
|
|
---
|
|
|
|
# Android Room
|
|
|
|
> 컴파일 타임 SQL 검증 + Flow 기반 reactive 쿼리. **Entity / DAO / Database** 3종. Migration 제대로 안 하면 사용자 데이터 손실 사고.
|
|
|
|
## 📖 핵심 개념
|
|
- Entity: @Entity 클래스 = 테이블.
|
|
- DAO: @Dao 인터페이스 = 쿼리 모음.
|
|
- Database: @Database — abstract class.
|
|
- Coroutines + Flow 자동 통합.
|
|
|
|
## 💻 코드 패턴
|
|
|
|
### Entity + DAO + DB
|
|
```kotlin
|
|
@Entity(tableName = "users", indices = [Index(value = ["email"], unique = true)])
|
|
data class UserEntity(
|
|
@PrimaryKey val id: String,
|
|
val email: String,
|
|
val name: String,
|
|
val createdAt: Long,
|
|
)
|
|
|
|
@Dao
|
|
interface UserDao {
|
|
@Query("SELECT * FROM users WHERE id = :id")
|
|
suspend fun findById(id: String): UserEntity?
|
|
|
|
@Query("SELECT * FROM users ORDER BY created_at DESC LIMIT :limit")
|
|
fun observeRecent(limit: Int): Flow<List<UserEntity>>
|
|
|
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
|
suspend fun upsert(user: UserEntity)
|
|
|
|
@Update
|
|
suspend fun update(user: UserEntity)
|
|
|
|
@Query("DELETE FROM users WHERE id = :id")
|
|
suspend fun delete(id: String)
|
|
}
|
|
|
|
@Database(entities = [UserEntity::class], version = 2, exportSchema = true)
|
|
abstract class AppDb : RoomDatabase() {
|
|
abstract fun userDao(): UserDao
|
|
}
|
|
```
|
|
|
|
### Migration
|
|
```kotlin
|
|
val MIGRATION_1_2 = object : Migration(1, 2) {
|
|
override fun migrate(db: SupportSQLiteDatabase) {
|
|
db.execSQL("ALTER TABLE users ADD COLUMN avatar_url TEXT")
|
|
}
|
|
}
|
|
|
|
Room.databaseBuilder(ctx, AppDb::class.java, "app.db")
|
|
.addMigrations(MIGRATION_1_2)
|
|
.build()
|
|
```
|
|
|
|
### Repository + Flow
|
|
```kotlin
|
|
class UserRepository(private val dao: UserDao, private val api: UserApi) {
|
|
fun observeUser(id: String): Flow<User> = dao.observeUser(id).map { it.toDomain() }
|
|
|
|
suspend fun refresh(id: String) {
|
|
val u = api.fetch(id)
|
|
dao.upsert(u.toEntity())
|
|
}
|
|
}
|
|
```
|
|
|
|
### Type converters
|
|
```kotlin
|
|
class Converters {
|
|
@TypeConverter fun fromInstant(t: Instant?): Long? = t?.toEpochMilli()
|
|
@TypeConverter fun toInstant(ms: Long?): Instant? = ms?.let { Instant.ofEpochMilli(it) }
|
|
}
|
|
|
|
@Database(entities = [...], version = 1)
|
|
@TypeConverters(Converters::class)
|
|
abstract class AppDb : RoomDatabase() { ... }
|
|
```
|
|
|
|
## 🤔 의사결정 기준
|
|
| 데이터 | 도구 |
|
|
|---|---|
|
|
| 구조화 데이터, 쿼리 필요 | Room |
|
|
| Key-value 설정 | DataStore |
|
|
| 큰 파일 | File API |
|
|
| 비밀 (token) | EncryptedSharedPreferences / Keystore |
|
|
| Sync (서버) | Room + WorkManager |
|
|
| 검색 | FTS5 (Room 지원) |
|
|
|
|
## ❌ 안티패턴
|
|
- **main thread 에서 DB 호출**: ANR. suspend / Flow 만.
|
|
- **migration 안 쓰고 fallbackToDestructiveMigration**: 사용자 데이터 손실.
|
|
- **DB schema 변경 후 version 안 올림**: crash on launch.
|
|
- **거대 트랜잭션 + 외부 API 호출 안에**: lock 길어짐.
|
|
- **Entity 노출 (UI 까지)**: domain 과 결합. mapper 권장.
|
|
- **인덱스 누락**: 큰 테이블 query 느림.
|
|
- **exportSchema = false**: schema 변경 검증 못 함. CI 에서 schema diff.
|
|
|
|
## 🤖 LLM 활용 힌트
|
|
- 신규 = Room. 단순 KV = DataStore.
|
|
- migration 매 version 마다.
|
|
- Flow + collectAsStateWithLifecycle 패턴.
|
|
|
|
## 🔗 관련 문서
|
|
- [[Android_DataStore_Patterns]]
|
|
- [[Android_Flow_StateFlow_SharedFlow]]
|