일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
Tags
- Android Universal Image Loader
- TabLayout and ViewPager
- 개발
- overridePendingTraction
- 독서
- FrameLayout
- 목표한번이뤄보자
- 2020년 목표
- FragmentPagerAdapter
- 재태크
- BottomNavigationViewEx
- FragmentSatePagerAdapter
- 운동
Archives
- Today
- Total
seops
[Read Docs] [Room] 1. About the local database 본문
[ 목차 ]
1. About the local database
- https://developer.android.com/training/data-storage/room?hl=ko
2. Define data using entities
3. Access data using DAOs
4. Define relationships between objects
5. Write asynchronous DAO
6. Create views into a databse
7. Prepopulat your database
8. Migrate your database
9. Test and debug your database
10. Reference complext data
Room은 SQLite를 이용하는데 있어, 원활한 데이터베이스 액세스가 가능하도록 SQLite의 추상화 계층을 제공합니다.
Room을 통해서 얻을 수 있점은 아래와 같습니다.
- 반복적이고 오류 발생 여지가 있는 코드를 줄이기 위한 Annotation
- 데이터베이스 마이그레이션 경로 간소화
아래 3가지는 Room의 기본 컴포넌트에 해당합니다. Database Class는 데이터베이스와 연결된 DAO 인스턴스를 제공합니다. 이를 이용하여 Enitity를 가져올 수 있고, Entity에 상응하는 테이블의 row로 업데이트하거나, 새로 추가할 수 있습니다.
- Database Class
- 데이터베이스를 보유합니다.
- 앱의 Persisted data(영구 데이터)의 기본 연결을 위한 Main Access Point 입니다.
- Entities
- 앱이 보유한 데이터베이스의 테이블을 나타냅니다.
- DAOs (Data Access Objects)
- 앱이 보유한 데이터베이스의 데이터를 query, update, insert, delete 할 수 있도록 메서드를 제공합니다.
코드 레벨에서 Room 컴포넌트의 활용 방법은 아래와 같습니다.
- Database Class
- AppDatabase 객체는 Singleton 패턴을 따라야 합니다.
- RoomDatabase 인스턴스는 리소스를 많이 소비합니다.
- 단일 프로세스에서 여러 인스턴스들을 접근하는 경우가 매우 드뭅니다.
- AppDatabase 객체는 Singleton 패턴을 따라야 합니다.
@Database(entities = [Client::class], version = 1)
abstract class AppDatabase: RoomDatabase() {
abstract fun clientDao(): ClientDao
}
@Entity
data class Client(
@PrimaryKey val id: Int,
@ColumnInfo val nickname: String,
@ColumnInfo val domain: String,
@ColumnInfo val email: String,
@ColumnInfo val password: String
)
@Dao
interface ClientDao {
@Insert
fun insertAll(vararg clients: Client)
@Delete
fun delete(client: Client)
@Query("SELECT * FROM client")
fun getAll(): List<Client>
}
Comments