3. Task 1: Create the RoomWordsSample app
https://codelabs.developers.google.com/codelabs/android-training-livedata-viewmodel/#2
Android fundamentals 10.1 Part A: Room, LiveData, and ViewModel
The data access object, or Dao, is an annotated class where you specify SQL queries and associate them with method calls. The compiler checks the SQL for errors, then generates queries from the annotations. For common queries, the libraries provide conveni
codelabs.developers.google.com
드디어 안드로이드 스튜디오를 실행할 때가 됬습니다. 일단 프로젝트를 만들어 봅시다.
샘플에 나온 프로젝트 제목은 RoomWordSample 이지만 이미 만들어둔 샘플이 있기 때문에 저는 일단 ViewmodelCodelap 이라는 이름으로 프로젝트를 만들었습니다.
그리고 dependencies를 추가해줍시다.
android app build.gradle 에 다음과같이 추가해 줍니다.
// Room components
implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion" annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion" androidTestImplementation "android.arch.persistence.room:testing:$rootProject.roomVersion" // Lifecycle components implementation "android.arch.lifecycle:extensions:$rootProject.archLifecycleVersion" annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archLifecycleVersion"
버전은 프로젝트 단에 추가해 줍니다.
ext { roomVersion = '1.1.1' archLifecycleVersion = '1.1.1' }
물론 버전이 더 높은 버전이 나와있긴 하지만 일단 귀찮으니 1.1.1 버전으로 통일해서 가겠습니다. 그리고 예제가 끝나지만 너무 짦으니 4까지 같이 가겠습니다.
4. Task 2: Create the Word entity
https://codelabs.developers.google.com/codelabs/android-training-livedata-viewmodel/#3
Android fundamentals 10.1 Part A: Room, LiveData, and ViewModel
The data access object, or Dao, is an annotated class where you specify SQL queries and associate them with method calls. The compiler checks the SQL for errors, then generates queries from the annotations. For common queries, the libraries provide conveni
codelabs.developers.google.com
이번 챕터에서는 Word Entity를 만들 겁니다. 이 앱의 데이터는 words, 이며 각각의 word는 데이터베이스 안의 Entity로 표현됩니다. 이러한 작업들은 Room 이 데이터베이스 테이블을 만들수 있도록 Word 클래스를 만들고 annotate 하는 작업입니다. 아래의 다이어그램은 데이터베이스 테이블을 보여주며 이 테이블은 하나의 word 컬럼을 가지고 있는데 이것은 primary Key역활을 하며 Hello와 World 2개의 행을 가지고 있습니다.
이제 정말 코드를 봅시다.
2.1 Create the Word class
@Entity(tableName = "word_table")
public class Word {
@PrimaryKey
@NonNull
@ColumnInfo(name = "word")
private String mWord;
public Word(@NonNull String word){this.mWord = word;}
public String getWord(){return this.mWord;}
}
@Entity 는 테이블의 이름이라고 할수 있습니다. 이걸 굳이 클래스 이름과 똑같이 할 필요는 없습니다. 클래스에 어노테이션으로 @Entity를 표기해줘야 Room은 이 클래스가 Entity라는걸 알수 있습니다.
@PrimaryKey 는 모든 Entity는 프라이머리키가 필요합니다. 이 앱에 있는 모든 단어들은 그 자신이 Primary key가 됩니다.
@NonNull 은 이 필드가 null값이 아니여야만 한다는걸 나타냅니다. 모든 Primary키는 Nonnull이어야 만 합니다.
@ColumnInfo 는 테이블의 컬럼 이름입니다. 이 이름이 꼭 클래스 이름과 같을 필요가 없습니다.
'안드로이드 > 공부' 카테고리의 다른 글
안드로이드 Room,LiveData,ViewModel 예제(Codelaps)-6 (0) | 2020.05.05 |
---|---|
안드로이드 Room,LiveData,ViewModel 예제(Codelaps)-4 (0) | 2020.05.05 |
안드로이드 Room,LiveData,ViewModel 예제(Codelaps)-2 (0) | 2020.05.01 |
안드로이드 Room,LiveData,ViewModel 예제(Codelaps)-1 (0) | 2020.04.30 |
안드로이드 라이브데이터(LiveData) (0) | 2020.04.30 |