Docs Menu
Docs Home
/ /
모델 데이터

속성 주석 - Flutter SDK

Realm 객체 모델의 속성에 기능을 추가하려면 주석을 사용합니다.

다트 에서 값 유형은 암시적으로 null을 허용하지 않지만 ?. 속성을 선택 사항으로 만들려면 ? 을 포함합니다.

class _Vehicle {
@PrimaryKey()
late ObjectId id;
late String? maybeDescription; // optional value
late double milesTravelled = 0; // 0 is default value
@Ignored()
late String notInRealmModel;
@Indexed()
late String make;
@MapTo('wheels') // 'wheels' is property name in the RealmObject
late int numberOfWheels;
}

내장된 언어 기능을 사용하여 속성에 기본값을 지정할 수 있습니다. 속성 선언에서 기본값을 지정합니다.

class _Vehicle {
@PrimaryKey()
late ObjectId id;
late String? maybeDescription; // optional value
late double milesTravelled = 0; // 0 is default value
@Ignored()
late String notInRealmModel;
@Indexed()
late String make;
@MapTo('wheels') // 'wheels' is property name in the RealmObject
late int numberOfWheels;
}

PrimaryKey 주석은 기본 키 속성 나타냅니다. 기본 키 영역 에 있는 객체 의 고유 식별자입니다. 동일한 유형의 다른 객체는 객체의 기본 키 주식 할 수 없습니다.

기본 키의 중요한 측면:

  • 영역에 객체를 추가한 후에는 기본 키를 변경할 수 없습니다.

  • RealmModel에서 하나의 속성에만 기본 키를 추가합니다.

  • String, int, ObjectIdUuid 유형만 기본 키가 될 수 있습니다.

  • Realm은 기본 키를 자동으로 인덱싱합니다.

  • 기본 키는 null을 허용합니다. null은 컬렉션에 있는 한 객체의 기본 키만 될 수 있습니다.

class _Vehicle {
@PrimaryKey()
late ObjectId id;
late String? maybeDescription; // optional value
late double milesTravelled = 0; // 0 is default value
@Ignored()
late String notInRealmModel;
@Indexed()
late String make;
@MapTo('wheels') // 'wheels' is property name in the RealmObject
late int numberOfWheels;
}

MapTo 주석은 모델 또는 속성 다른 이름으로 유지해야 함을 나타냅니다. 코드 스타일 규칙이 다를 수 있는 다양한 바인딩에서 Realm 열 때 유용합니다. 예시 를 들면 다음과 같습니다.

  • 명명 규칙이 서로 다른 플랫폼에서 더 쉽게 작업할 수 있습니다. 예를 들면 Device Sync 스키마 속성 이름은 스네이크 표기법을 사용하고, 프로젝트는 카멜 표기법을 사용하는 경우입니다.

  • 마이그레이션을 강제하지 않고 클래스 또는 필드 이름을 변경합니다.

@RealmModel()
@MapTo('naval_ship')
class _Boat {
@PrimaryKey()
late ObjectId id;
late String name;
late int? maxKnots;
late int? nauticalMiles;
}
class _Vehicle {
@PrimaryKey()
late ObjectId id;
late String? maybeDescription; // optional value
late double milesTravelled = 0; // 0 is default value
@Ignored()
late String notInRealmModel;
@Indexed()
late String make;
@MapTo('wheels') // 'wheels' is property name in the RealmObject
late int numberOfWheels;
}

의 속성 에 Ignored 주석을 RealmModel 추가하면 영역 객체 생성기는 RealmObject 스키마 에 속성 포함하거나 Realm 에 유지하지 않습니다.

class _Vehicle {
@PrimaryKey()
late ObjectId id;
late String? maybeDescription; // optional value
late double milesTravelled = 0; // 0 is default value
@Ignored()
late String notInRealmModel;
@Indexed()
late String make;
@MapTo('wheels') // 'wheels' is property name in the RealmObject
late int numberOfWheels;
}

Indexed 주석을 추가하여 필드에 인덱스 만듭니다. 인덱스를 사용하면 쓰기 (write) 시간이 약간 느려지고 저장 및 메모리 오버헤드 비용 , 일부 쿼리 속도를 크게 높일 수 있습니다. Realm 인덱스를 디스크에 저장하므로 영역 파일이 더 커집니다. 각 인덱스 항목 최소 12 바이트입니다. 인덱스는 null을 허용할 수 있습니다.

인덱싱할 수 있는 데이터 유형은 다음과 같습니다.

  • bool

  • int

  • String

  • ObjectId

  • Uuid

  • DateTime

  • RealmValue

class _Vehicle {
@PrimaryKey()
late ObjectId id;
late String? maybeDescription; // optional value
late double milesTravelled = 0; // 0 is default value
@Ignored()
late String notInRealmModel;
@Indexed()
late String make;
@MapTo('wheels') // 'wheels' is property name in the RealmObject
late int numberOfWheels;
}

Realm은 표준 인덱스 외에도 문자열 속성에 대한 FTS(Full-Text Search) 인덱스도 지원합니다. 표준 인덱스 유무에 관계없이 문자열 필드를 쿼리할 수 있지만 FTS 인덱스를 사용하면 여러 단어와 구를 검색하고 나머지는 제외할 수 있습니다.

FTS 인덱스 쿼리에 대한 자세한 내용은 Full Text Search를 참조하세요.

속성 에 FTS 인덱스 만들려면 @Indexed 주석을 사용하고 RealmIndexTypefullText로 지정합니다. 이렇게 하면 속성 에 대한 전체 텍스트 쿼리가 가능합니다. 다음 예시 에서는 FTS 주석을 사용하여 패턴 과 재료 속성을 표시합니다.

@RealmModel()
class _Rug {
@PrimaryKey()
late ObjectId id;
@Indexed(RealmIndexType.fullText)
late String pattern;
@Indexed(RealmIndexType.fullText)
late String material;
late int softness;
}

돌아가기

관계

이 페이지의 내용