Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- AppBarLayout
- LiveData
- activity
- kotlin
- Coroutine
- CustomView
- Navigation
- 코틀린
- room
- onMeasure
- onLayout
- Algorithm
- Behavior
- notification
- lifecycle
- ViewModel
- hilt
- 알고리즘
- sqlite
- 안드로이드
- CoordinatorLayout
- 백준
- DataBinding
- CollapsingToolbarLayout
- BOJ
- Android
- View
- 알림
- recyclerview
- HTTP
Archives
- Today
- Total
개발일지
Android in A..Z - Room (Minimal) 본문
Minimal
Entity로 선언된 객체에 A, B, C, D, E 5개의 변수가 있고, 5개가 Room에 저장됐다고 가정하자 이 때 필요한 정보는 A, B 두개뿐인데 A, B, C, D, E 5개 전부 불러와서 변수에 저장하면 리소스가 낭비된다.
=> Minimal한 객체를 만들고 Minimal로 받아오자
ToDo
@Entity(
foreignKeys = [
ForeignKey(
entity = Drawer::class,
parentColumns = ["name"],
childColumns = ["drawerName"],
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE
)
]
)
data class ToDo(
@PrimaryKey(autoGenerate = true)
var id: Long = 0L,
var text: String = "",
var drawerName: String = "",
var writtenTime: Date = Date(),
var isFinished: Boolean = false
)
ToDoMinimal
@Entity를 선언할 필요가 없으며 필요한 정보만 변수로 선언한다.
data class ToDoMinimal(
var id: Long = 0L,
var text: String = "",
)
ToDoDao
반환형만 ToDoMinimal로 하면 Room에서 내부적으로 데이터를 처리하여 ToDoMinimal로 반환한다.
@Dao
interface ToDoDao : BaseDao<ToDo> {
@Transaction
@Query("SELECT * FROM ToDo WHERE drawerName = :drawerName")
fun findLiveDataByDrawerName(drawerName: String): LiveData<MutableList<ToDo>>
@Transaction
@Query("SELECT * FROM ToDo WHERE :begin <= writtenTime AND writtenTime <= :end")
fun findLiveDateByRange(begin: Date, end: Date): LiveData<MutableList<ToDo>>
@Transaction
@Query("SELECT * FROM ToDo")
fun findLiveDataMinimal(): LiveData<MutableList<ToDoMinimal>>
}
Git (예제코드)
github.com/KangTaeJong98/Example/tree/main/Android/Room
KangTaeJong98/Example
My Example Code. Contribute to KangTaeJong98/Example development by creating an account on GitHub.
github.com
'Android (안드로이드) > Room' 카테고리의 다른 글
Android in A..Z - Room (TypeConvert) (1) | 2021.01.17 |
---|---|
Android in A..Z - Room (allowMainThreadQueries) (0) | 2021.01.17 |
Android in A..Z - Room (관계) (0) | 2021.01.17 |
Android in A..Z - Room (Migration) (0) | 2021.01.17 |
Android in A..Z - Room (데이터 미리 채우기) (0) | 2021.01.17 |
Comments