SlideShare a Scribd company logo
1 of 15
Download to read offline
0
Realm
모델 & 관계
Made by 이종찬
1
01 모델(Model)
지원되는 필드 타입들을 알아보자.
박스형
Boolean, Byte, Short, Integer, Long, Float, Double
-> 박스형과 RealmObject형 필드만 null 값을 넣을 수 있다.
서브클래스 및 리스트형
RealmObject의 서브클래스,
RealmList <? Extends RealmObject>
기본형
boolean, short, int, long, float, double, String, Date, byte[]
-> short, int, long 은 Realm 내에서 모두 long으로 대응된다.
1
2
3
Field
2
01 모델(Model)
지원되는 필드 타입들을 알아보자.
Field
public class Dog extends RealmObject {
public String name;
public int age;
}
public RealmList<Dog> dogs;
3
01 모델(Model)
Realm에서 사용되는 어노테이션(annotation)을 알아보자.
Annotation
1 @Required
1 null 값을 허용하지 않기 위해 사용한다.
2 박스형만 지정할 수 있다.
3 기본형과 RealmList는 암묵적으로 Required로 취급한다.
4 RealmObject는 항상 널이 가능
2 @Ignore
1 필드가 디스크에 저장되지 않도록 하는 기능을 한다.
2 사용자의 입력이 모델의 필드보다 많을 때,
쓰지 않을 데이터 필드를 다룰 때 사용한다.
4
01 모델(Model)
Realm에서 사용되는 어노테이션(annotation)을 알아보자.
Annotation
3 @Index
1 해당 필드에 검색 색인을 추가한다.
2 질의 처리를 빠르게 할 수 있다.
3 삽입 처리가 느리고 데이터의 파일을 크게 만든다.
4 읽기에 최적화를 하는 경우에만 추가하는 것을 추천한다.
5 String, byte, short, int, long, Boolean, Date에 가능하다.
5
01 모델(Model)
Realm에서 사용되는 어노테이션(annotation)을 알아보자.
Annotation
4 @PrimaryKey
1 문자열(String)이나 정수(byte, short, int, long)이나 이에
대응하는 박스형(Byte, Short, Integer, Long)에 가능하다.
2 여러 필드를 PrimaryKey로 설정하는 것은 불가능하다.
3 @PrimaryKey는 암묵적으로 @Index를 설정한다.
4 PrimaryKey를 사용하면 Querying의 속도는 향상될 수 있다.
5 객체를 생성하고 수정할 때 속도하락이 있을 수 있다.
6 String, byte, short, int, long, Boolean, Date 에 가능하다.
6
01 모델(Model)
RealmObject의 사용법을 알아보자.
Object
1 일반적으로 사용하기
public class Dog extends RealmObject {
private String name;
public void setName(String name) { this.name = name; }
public String getName() { return name; }
}
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Dog dog = realm.createObject(Dog.class);
dog.setName("Fido");
}
});
7
01 모델(Model)
RealmObject의 사용법을 알아보자.
Object
2 POJO(Plain Old Java Object)처럼 사용하기
public class Dog extends RealmObject {
public String name;
}
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Dog dog = realm.createObject(Dog.class);
dog.name = “Fido”;
}
});
요구사항에 맞춰서 getter와 setter에 로직을 추가할 수도 있다.
8
POJO(Plain Old Java Object)란?
POJO : 프레임워크 등에 종속된 ‘무거운’ 객체를
만들게 된 것에(EJB에) 반발해서 사용하게 된 용어
POJO의 필수요소
1. light-weight(possibly) : 가볍게
2. flexible : 유연성
3. simple : 간단 명료
4. supported by separate optional components
such as hibernate or spring
POJO가 아닌 대표적인 객체
public HelloSevlet extends HttpServlet{...}
-> 자바 서블릿 코드를 작성할 때는 반드시 HttpServlet을 상속받아야한다.
-> 서플릿 프로그래밍을 한다는 이유로 상속이라는 핵심 기능을 빼앗긴 것
-> 개발자가 직접 상속을 할 수 있는 기회가 없어진 것이다.
-> HttpServlet 을 상속 받았기 때문에 이 코드를 이해해야 하며
어떤 기능이 있는지 어떻게 재사용해야 할지 판단하기도 어렵다.
9
01 모델(Model)
현재 Realm에 어떠한 제약사항이 있는지 알아보자.
제약사항
RealmObject 외의 상속을 허용하지 않는다.
기본 생성자는 재정의 하지 말아야 한다.
-> 기본 생성자에서 Realm 인스턴스가 주어져 있다고 가정하는
메소드를 호출하기 때문이다.
-> RealmModel 인터페이스를 추가하는 방식으로 해결 할 수도 있다.
final, transient, volatile 지원하지 않는다.
Realm에 의해 관리되는 객체와 비관리 객체 간의
불일치를 막기 위해서입니다.
1
2
10
02 관계(Relationships)
Realm에서 관계는 어떻게 정의하는지 알아보자.
1
다 대 일
다 대 일
public class Book extends RealmObject {
private String title;
// 다른 필드 및 getter, setter
}
public class Author extends RealmObject {
private Book book;
// 다른 필드 및 getter, setter
}
각 Author은 0혹은 1개의 Book을 갖는다.
하지만 책에는 공동 저자 라는 것이 있다.
즉, 여러 Author가 하나의 Book을 사용할 수 있다.
이러한 관계가 다-대-일 이다.
author1
author2
author3
author4
book1
book2
11
02 관계(Relationships)
Realm에서 관계는 어떻게 정의하는지 알아보자.
2
다 대 다
다 대 다
public class Book extends RealmObject {
private String title;
// 다른 필드 및 getter, setter
}
public class Author extends RealmObject {
private RealmList<Book> books;
// 다른 필드 및 getter, setter
}
Author는 여러 권의 Book을 집필 했을 수 있다.
이때 또한 공동 저자가 적용 될 수 있다.
다-대-다 관계이다.
RealmList를 이용하면 다-대-다 관계를 설정할 수 있다.
author1
author2
author3
author4
book1
book2
book3
12
02 관계(Relationships)
Realm에서 관계는 어떻게 정의하는지 알아보자.
2
다 대 다
다 대 다
RealmList는 자바의 List와 비슷한 행동을 한다.
RealmList안에 객체를 추가하기 위해서는 RealmList.add()를 사용하면 된다.
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Author author = realm. createObject(Author.class);
Book book = realm.createObject(Book.class);
Book.settitle(“realm”);
author.books.add(book);
}
});
13
RealmList의 사용
RealmList를 사용하여 재귀적인 관계도 설정 가능하다.
public class Person extends RealmObject {
private RealmList<Person> friends;
// 다른 필드 및 getter, setter
}
RealmList 필드에 null 값을 설정하면 리스트가 초기화된다.
이것은 null로 바꾸는 것이 아닌 길이가 0인 리스트로 만드는 것이다.
때문에 RealmList의 getter는 null을 절대 반환하지 않는다.
길이가 0인 리스트를 반환한다.
14
감사합니다.
Made by 이종찬
정보 출처 : realm.io
ppt템플릿 : 홍양홍삼님 블로그

More Related Content

What's hot

[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttpNAVER D2
 
Objective-C Runtime Programming Guide
Objective-C Runtime Programming GuideObjective-C Runtime Programming Guide
Objective-C Runtime Programming GuideSung-Kwan Kim
 
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http RequestNAVER D2
 
Secrets of the JavaScript Ninja - Chapter 3. Functions are Fundamental
Secrets of the JavaScript Ninja - Chapter 3. Functions are FundamentalSecrets of the JavaScript Ninja - Chapter 3. Functions are Fundamental
Secrets of the JavaScript Ninja - Chapter 3. Functions are FundamentalHyuncheol Jeon
 
Api design for c++ pattern
Api design for c++ patternApi design for c++ pattern
Api design for c++ patternjinho park
 
동기화, 스케줄링
동기화, 스케줄링동기화, 스케줄링
동기화, 스케줄링xxbdxx
 
파이썬 class 및 function namespace 이해하기
파이썬 class 및 function namespace 이해하기파이썬 class 및 function namespace 이해하기
파이썬 class 및 function namespace 이해하기Yong Joon Moon
 
자바야 놀자 PPT
자바야 놀자 PPT자바야 놀자 PPT
자바야 놀자 PPTJinKyoungHeo
 
파이썬 프로퍼티 디스크립터 이해하기
파이썬 프로퍼티 디스크립터 이해하기파이썬 프로퍼티 디스크립터 이해하기
파이썬 프로퍼티 디스크립터 이해하기Yong Joon Moon
 
주니어 개발자도 이해 할 수 있는 Go - Scope 편
주니어 개발자도 이해 할 수 있는 Go - Scope 편주니어 개발자도 이해 할 수 있는 Go - Scope 편
주니어 개발자도 이해 할 수 있는 Go - Scope 편Darion Kim
 
Malzilla tutorial1
Malzilla tutorial1Malzilla tutorial1
Malzilla tutorial1re4lfl0w
 
주니어 개발자도 이해 할 수 있는 Go - Namespace 편
주니어 개발자도 이해 할 수 있는 Go - Namespace 편주니어 개발자도 이해 할 수 있는 Go - Namespace 편
주니어 개발자도 이해 할 수 있는 Go - Namespace 편Darion Kim
 
Scala type class pattern
Scala type class patternScala type class pattern
Scala type class patternYong Joon Moon
 
파이썬 파일처리 이해하기
파이썬 파일처리 이해하기파이썬 파일처리 이해하기
파이썬 파일처리 이해하기Yong Joon Moon
 
Java 강의자료 ed11
Java 강의자료 ed11Java 강의자료 ed11
Java 강의자료 ed11hungrok
 
Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Realm은 어떻게 효율적인 데이터베이스를 만들었나?Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Realm은 어떻게 효율적인 데이터베이스를 만들었나?Leonardo YongUk Kim
 
Scope and Closure of JavaScript
Scope and Closure of JavaScript Scope and Closure of JavaScript
Scope and Closure of JavaScript Dahye Kim
 

What's hot (20)

[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - OkHttp
 
Javascript
JavascriptJavascript
Javascript
 
Objective-C Runtime Programming Guide
Objective-C Runtime Programming GuideObjective-C Runtime Programming Guide
Objective-C Runtime Programming Guide
 
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request
[D2 CAMPUS] 안드로이드 오픈소스 스터디자료 - Http Request
 
Secrets of the JavaScript Ninja - Chapter 3. Functions are Fundamental
Secrets of the JavaScript Ninja - Chapter 3. Functions are FundamentalSecrets of the JavaScript Ninja - Chapter 3. Functions are Fundamental
Secrets of the JavaScript Ninja - Chapter 3. Functions are Fundamental
 
Api design for c++ pattern
Api design for c++ patternApi design for c++ pattern
Api design for c++ pattern
 
동기화, 스케줄링
동기화, 스케줄링동기화, 스케줄링
동기화, 스케줄링
 
파이썬 class 및 function namespace 이해하기
파이썬 class 및 function namespace 이해하기파이썬 class 및 function namespace 이해하기
파이썬 class 및 function namespace 이해하기
 
자바야 놀자 PPT
자바야 놀자 PPT자바야 놀자 PPT
자바야 놀자 PPT
 
파이썬 프로퍼티 디스크립터 이해하기
파이썬 프로퍼티 디스크립터 이해하기파이썬 프로퍼티 디스크립터 이해하기
파이썬 프로퍼티 디스크립터 이해하기
 
주니어 개발자도 이해 할 수 있는 Go - Scope 편
주니어 개발자도 이해 할 수 있는 Go - Scope 편주니어 개발자도 이해 할 수 있는 Go - Scope 편
주니어 개발자도 이해 할 수 있는 Go - Scope 편
 
Malzilla tutorial1
Malzilla tutorial1Malzilla tutorial1
Malzilla tutorial1
 
Java inner class
Java inner classJava inner class
Java inner class
 
주니어 개발자도 이해 할 수 있는 Go - Namespace 편
주니어 개발자도 이해 할 수 있는 Go - Namespace 편주니어 개발자도 이해 할 수 있는 Go - Namespace 편
주니어 개발자도 이해 할 수 있는 Go - Namespace 편
 
Scala type class pattern
Scala type class patternScala type class pattern
Scala type class pattern
 
파이썬 파일처리 이해하기
파이썬 파일처리 이해하기파이썬 파일처리 이해하기
파이썬 파일처리 이해하기
 
Java 강의자료 ed11
Java 강의자료 ed11Java 강의자료 ed11
Java 강의자료 ed11
 
4-1. javascript
4-1. javascript4-1. javascript
4-1. javascript
 
Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Realm은 어떻게 효율적인 데이터베이스를 만들었나?Realm은 어떻게 효율적인 데이터베이스를 만들었나?
Realm은 어떻게 효율적인 데이터베이스를 만들었나?
 
Scope and Closure of JavaScript
Scope and Closure of JavaScript Scope and Closure of JavaScript
Scope and Closure of JavaScript
 

Viewers also liked

간단한 회원가입 형태 만들기
간단한 회원가입 형태 만들기간단한 회원가입 형태 만들기
간단한 회원가입 형태 만들기Lee-Jong-Chan
 
Realm: 초고속 데이터베이스
Realm: 초고속 데이터베이스Realm: 초고속 데이터베이스
Realm: 초고속 데이터베이스Leonardo YongUk Kim
 
[1B6]Realm a database for android & ios
[1B6]Realm a database for android & ios[1B6]Realm a database for android & ios
[1B6]Realm a database for android & iosNAVER D2
 
진행중인 프로젝트
진행중인 프로젝트진행중인 프로젝트
진행중인 프로젝트Lee-Jong-Chan
 
토이 프로젝트 메모장
토이 프로젝트   메모장토이 프로젝트   메모장
토이 프로젝트 메모장Lee-Jong-Chan
 
우선 순위 메모장
우선 순위 메모장우선 순위 메모장
우선 순위 메모장Lee-Jong-Chan
 
Touch event 다루기
Touch event 다루기Touch event 다루기
Touch event 다루기Lee-Jong-Chan
 
코딩 생산성 높이기
코딩 생산성 높이기코딩 생산성 높이기
코딩 생산성 높이기Lee-Jong-Chan
 
T아카데미 aws 수강 리뷰
T아카데미 aws 수강 리뷰T아카데미 aws 수강 리뷰
T아카데미 aws 수강 리뷰Lee-Jong-Chan
 
Painless Persistence with Realm
Painless Persistence with RealmPainless Persistence with Realm
Painless Persistence with RealmChristian Melchior
 
Realm Java for Android
Realm Java for AndroidRealm Java for Android
Realm Java for AndroidGokhan Arik
 
Realm.io for iOS
Realm.io for iOSRealm.io for iOS
Realm.io for iOSEunjoo Im
 
프알못의 Realm 사용기
프알못의 Realm 사용기프알못의 Realm 사용기
프알못의 Realm 사용기Mijeong Jeon
 
Painless Persistence in a Disconnected World
Painless Persistence in a Disconnected WorldPainless Persistence in a Disconnected World
Painless Persistence in a Disconnected WorldChristian Melchior
 
파크히어 Realm 사용 사례
파크히어 Realm 사용 사례파크히어 Realm 사용 사례
파크히어 Realm 사용 사례선협 이
 
오픈 소스로 취업하기: 나는 어떻게 오픈 소스를 하다 렘 개발자가 되었나?
오픈 소스로 취업하기: 나는 어떻게 오픈 소스를 하다 렘 개발자가 되었나?오픈 소스로 취업하기: 나는 어떻게 오픈 소스를 하다 렘 개발자가 되었나?
오픈 소스로 취업하기: 나는 어떻게 오픈 소스를 하다 렘 개발자가 되었나?Leonardo YongUk Kim
 

Viewers also liked (19)

간단한 회원가입 형태 만들기
간단한 회원가입 형태 만들기간단한 회원가입 형태 만들기
간단한 회원가입 형태 만들기
 
Realm: 초고속 데이터베이스
Realm: 초고속 데이터베이스Realm: 초고속 데이터베이스
Realm: 초고속 데이터베이스
 
[1B6]Realm a database for android & ios
[1B6]Realm a database for android & ios[1B6]Realm a database for android & ios
[1B6]Realm a database for android & ios
 
진행중인 프로젝트
진행중인 프로젝트진행중인 프로젝트
진행중인 프로젝트
 
토이 프로젝트 메모장
토이 프로젝트   메모장토이 프로젝트   메모장
토이 프로젝트 메모장
 
Context
ContextContext
Context
 
우선 순위 메모장
우선 순위 메모장우선 순위 메모장
우선 순위 메모장
 
Touch event 다루기
Touch event 다루기Touch event 다루기
Touch event 다루기
 
코딩 생산성 높이기
코딩 생산성 높이기코딩 생산성 높이기
코딩 생산성 높이기
 
T아카데미 aws 수강 리뷰
T아카데미 aws 수강 리뷰T아카데미 aws 수강 리뷰
T아카데미 aws 수강 리뷰
 
Painless Persistence with Realm
Painless Persistence with RealmPainless Persistence with Realm
Painless Persistence with Realm
 
Realm Java for Android
Realm Java for AndroidRealm Java for Android
Realm Java for Android
 
Realm.io for iOS
Realm.io for iOSRealm.io for iOS
Realm.io for iOS
 
프알못의 Realm 사용기
프알못의 Realm 사용기프알못의 Realm 사용기
프알못의 Realm 사용기
 
Painless Persistence in a Disconnected World
Painless Persistence in a Disconnected WorldPainless Persistence in a Disconnected World
Painless Persistence in a Disconnected World
 
Realm @Android
Realm @Android Realm @Android
Realm @Android
 
파크히어 Realm 사용 사례
파크히어 Realm 사용 사례파크히어 Realm 사용 사례
파크히어 Realm 사용 사례
 
오픈 소스로 취업하기: 나는 어떻게 오픈 소스를 하다 렘 개발자가 되었나?
오픈 소스로 취업하기: 나는 어떻게 오픈 소스를 하다 렘 개발자가 되었나?오픈 소스로 취업하기: 나는 어떻게 오픈 소스를 하다 렘 개발자가 되었나?
오픈 소스로 취업하기: 나는 어떻게 오픈 소스를 하다 렘 개발자가 되었나?
 
Responsabilidad social editado
Responsabilidad social editadoResponsabilidad social editado
Responsabilidad social editado
 

02 realm 모델 &amp; 관계

  • 2. 1 01 모델(Model) 지원되는 필드 타입들을 알아보자. 박스형 Boolean, Byte, Short, Integer, Long, Float, Double -> 박스형과 RealmObject형 필드만 null 값을 넣을 수 있다. 서브클래스 및 리스트형 RealmObject의 서브클래스, RealmList <? Extends RealmObject> 기본형 boolean, short, int, long, float, double, String, Date, byte[] -> short, int, long 은 Realm 내에서 모두 long으로 대응된다. 1 2 3 Field
  • 3. 2 01 모델(Model) 지원되는 필드 타입들을 알아보자. Field public class Dog extends RealmObject { public String name; public int age; } public RealmList<Dog> dogs;
  • 4. 3 01 모델(Model) Realm에서 사용되는 어노테이션(annotation)을 알아보자. Annotation 1 @Required 1 null 값을 허용하지 않기 위해 사용한다. 2 박스형만 지정할 수 있다. 3 기본형과 RealmList는 암묵적으로 Required로 취급한다. 4 RealmObject는 항상 널이 가능 2 @Ignore 1 필드가 디스크에 저장되지 않도록 하는 기능을 한다. 2 사용자의 입력이 모델의 필드보다 많을 때, 쓰지 않을 데이터 필드를 다룰 때 사용한다.
  • 5. 4 01 모델(Model) Realm에서 사용되는 어노테이션(annotation)을 알아보자. Annotation 3 @Index 1 해당 필드에 검색 색인을 추가한다. 2 질의 처리를 빠르게 할 수 있다. 3 삽입 처리가 느리고 데이터의 파일을 크게 만든다. 4 읽기에 최적화를 하는 경우에만 추가하는 것을 추천한다. 5 String, byte, short, int, long, Boolean, Date에 가능하다.
  • 6. 5 01 모델(Model) Realm에서 사용되는 어노테이션(annotation)을 알아보자. Annotation 4 @PrimaryKey 1 문자열(String)이나 정수(byte, short, int, long)이나 이에 대응하는 박스형(Byte, Short, Integer, Long)에 가능하다. 2 여러 필드를 PrimaryKey로 설정하는 것은 불가능하다. 3 @PrimaryKey는 암묵적으로 @Index를 설정한다. 4 PrimaryKey를 사용하면 Querying의 속도는 향상될 수 있다. 5 객체를 생성하고 수정할 때 속도하락이 있을 수 있다. 6 String, byte, short, int, long, Boolean, Date 에 가능하다.
  • 7. 6 01 모델(Model) RealmObject의 사용법을 알아보자. Object 1 일반적으로 사용하기 public class Dog extends RealmObject { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } } realm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { Dog dog = realm.createObject(Dog.class); dog.setName("Fido"); } });
  • 8. 7 01 모델(Model) RealmObject의 사용법을 알아보자. Object 2 POJO(Plain Old Java Object)처럼 사용하기 public class Dog extends RealmObject { public String name; } realm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { Dog dog = realm.createObject(Dog.class); dog.name = “Fido”; } }); 요구사항에 맞춰서 getter와 setter에 로직을 추가할 수도 있다.
  • 9. 8 POJO(Plain Old Java Object)란? POJO : 프레임워크 등에 종속된 ‘무거운’ 객체를 만들게 된 것에(EJB에) 반발해서 사용하게 된 용어 POJO의 필수요소 1. light-weight(possibly) : 가볍게 2. flexible : 유연성 3. simple : 간단 명료 4. supported by separate optional components such as hibernate or spring POJO가 아닌 대표적인 객체 public HelloSevlet extends HttpServlet{...} -> 자바 서블릿 코드를 작성할 때는 반드시 HttpServlet을 상속받아야한다. -> 서플릿 프로그래밍을 한다는 이유로 상속이라는 핵심 기능을 빼앗긴 것 -> 개발자가 직접 상속을 할 수 있는 기회가 없어진 것이다. -> HttpServlet 을 상속 받았기 때문에 이 코드를 이해해야 하며 어떤 기능이 있는지 어떻게 재사용해야 할지 판단하기도 어렵다.
  • 10. 9 01 모델(Model) 현재 Realm에 어떠한 제약사항이 있는지 알아보자. 제약사항 RealmObject 외의 상속을 허용하지 않는다. 기본 생성자는 재정의 하지 말아야 한다. -> 기본 생성자에서 Realm 인스턴스가 주어져 있다고 가정하는 메소드를 호출하기 때문이다. -> RealmModel 인터페이스를 추가하는 방식으로 해결 할 수도 있다. final, transient, volatile 지원하지 않는다. Realm에 의해 관리되는 객체와 비관리 객체 간의 불일치를 막기 위해서입니다. 1 2
  • 11. 10 02 관계(Relationships) Realm에서 관계는 어떻게 정의하는지 알아보자. 1 다 대 일 다 대 일 public class Book extends RealmObject { private String title; // 다른 필드 및 getter, setter } public class Author extends RealmObject { private Book book; // 다른 필드 및 getter, setter } 각 Author은 0혹은 1개의 Book을 갖는다. 하지만 책에는 공동 저자 라는 것이 있다. 즉, 여러 Author가 하나의 Book을 사용할 수 있다. 이러한 관계가 다-대-일 이다. author1 author2 author3 author4 book1 book2
  • 12. 11 02 관계(Relationships) Realm에서 관계는 어떻게 정의하는지 알아보자. 2 다 대 다 다 대 다 public class Book extends RealmObject { private String title; // 다른 필드 및 getter, setter } public class Author extends RealmObject { private RealmList<Book> books; // 다른 필드 및 getter, setter } Author는 여러 권의 Book을 집필 했을 수 있다. 이때 또한 공동 저자가 적용 될 수 있다. 다-대-다 관계이다. RealmList를 이용하면 다-대-다 관계를 설정할 수 있다. author1 author2 author3 author4 book1 book2 book3
  • 13. 12 02 관계(Relationships) Realm에서 관계는 어떻게 정의하는지 알아보자. 2 다 대 다 다 대 다 RealmList는 자바의 List와 비슷한 행동을 한다. RealmList안에 객체를 추가하기 위해서는 RealmList.add()를 사용하면 된다. realm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { Author author = realm. createObject(Author.class); Book book = realm.createObject(Book.class); Book.settitle(“realm”); author.books.add(book); } });
  • 14. 13 RealmList의 사용 RealmList를 사용하여 재귀적인 관계도 설정 가능하다. public class Person extends RealmObject { private RealmList<Person> friends; // 다른 필드 및 getter, setter } RealmList 필드에 null 값을 설정하면 리스트가 초기화된다. 이것은 null로 바꾸는 것이 아닌 길이가 0인 리스트로 만드는 것이다. 때문에 RealmList의 getter는 null을 절대 반환하지 않는다. 길이가 0인 리스트를 반환한다.
  • 15. 14 감사합니다. Made by 이종찬 정보 출처 : realm.io ppt템플릿 : 홍양홍삼님 블로그