문서 메뉴

문서 홈애플리케이션 개발Atlas Device SDK

사전 - .NET SDK

이 페이지의 내용

  • 개요
  • 사전 유형
  • 사용 예시
  • 변화 관찰하기

버전 10.2.0에 추가됨

Realm 딕셔너리는 IDictionary 의 구현입니다. 유형의 String 키를 가지고 있으며 collection제외한 모든 Realm 유형 의 값을 지원합니다. 사전을 정의하려면 게터 IDictionary<string, TValue> 전용 속성을 사용하며, 여기서 TValue 는 지원되는 유형 중 하나입니다.

객체 사전에는 null 객체가 포함될 수 있습니다. 마찬가지로 기본 유형의 사전에도 null 값이 포함될 수 있습니다. 사전에 null 값을 허용하지 않으려면 사전 선언에 null이 아닌 유형을 사용하거나(예: RealmObject 대신 IDictionary<string, double?> 사용), 사전 선언에 null이 아닌 유형을 사용하세요. 이전 스키마 유형 정의를 사용 중이거나(클래스가 RealmObject 기본 클래스에서 파생됨) 무효화 기능을 사용하지 않는 경우, 사전에 string 또는 byte[]와 같이 null 허용한 참조 유형이 포함된 경우 [필수] 속성을 사용하세요.

Realm에서는 지도 키에 . 또는 $ 문자를 사용할 수 없습니다. 백분율 인코딩 및 디코딩을 사용하여 허용되지 않는 문자 중 하나가 포함된 지도 키를 저장할 수 있습니다.

중요

동기화에서 지원되지 않는 null 허용 값

로컬 전용 영역은 null 허용(선택 사항) 값 컬렉션을 지원하지만 Sync는 지원하지 않습니다.

다음 코드에서는 사전 형식의 예를 보여 줍니다.

public partial class Inventory : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public string Id { get; set; }
// The key must be of type string; the value can be
// of any Realm-supported type, including objects
// that inherit from RealmObject or EmbeddedObject
public IDictionary<string, Plant?> Plants { get; }
public IDictionary<string, bool> BooleansDictionary { get; }
// Nullable types are supported in local-only
// Realms, but not with Sync
public IDictionary<string, int?> NullableIntDictionary { get; }
public IDictionary<string, string> RequiredStringsDictionary { get; }
}

다음 코드는 문자열 쿼리(RQL) 또는 LINQ를 사용하여 사전을 만들고, 쓰고, 사전에서 읽는 방법을 보여 줍니다.

var storeInventory = new Inventory()
{
Id = ObjectId.GenerateNewId().ToString()
};
storeInventory.Plants.Add("Petunia", new Plant());
storeInventory.NullableIntDictionary.Add("random things", 7);
storeInventory.RequiredStringsDictionary.Add("foo", "bar");
var storeInventory2 = new Inventory()
{
Id = ObjectId.GenerateNewId().ToString()
};
storeInventory2.RequiredStringsDictionary.Add("foo", "Bar");
realm.Write(() =>
{
realm.Add(storeInventory);
realm.Add(storeInventory2);
});
// Find all Inventory items that have "Petunia"
// as a key in their Plants dictionary.
var petunias = realm.All<Inventory>()
.Filter("Plants.@keys == 'Petunia'");
// Find all Inventory items that have at least one value in their
// IntDictionary that is larger than 5 using RQL
var matchesMoreThanFive = realm.All<Inventory>()
.Filter("NullableIntDictionary.@values > 5");
// Find all Inventory items where the RequiredStringsDictionary has a key
// "Foo", and the value of that key contains the phrase "bar"
// (case insensitive)
var matches = realm.All<Inventory>()
.Filter("RequiredStringsDictionary['foo'] CONTAINS[c] 'bar'");
// matches.Count() == 2
// Query the Plants dictionary of an Inventory object
// for a specific plant
var myStoreInventory = realm
.All<Inventory>().FirstOrDefault();
var petunia = myStoreInventory.Plants.AsRealmQueryable()
.Where(p => p.Name == "Petunia");

사전에서 INotifyCollectionChanged.CollectionChanged 이벤트를 사용하여 컬렉션의 변경 내용을 감시하고 INotifyPropertyChanged.PropertyChanged 이벤트를 사용하여 사전의 특정 속성에 대한 변경 내용을 감시할 수 있습니다.

다음 코드 예시에는 IntDictionary라는 이름의 IDictionary<string, int> 속성을 가진 클래스가 있습니다. CollectionChangedPropertyChanged 이벤트 모두에 대해 이벤트 핸들러를 설정합니다.

var dictionary = container.IntDictionary.AsRealmCollection();
dictionary.CollectionChanged += (sender, e) =>
{
Console.WriteLine($"Collection {sender} changed: {e.Action}");
};
dictionary.PropertyChanged += (sender, e) =>
{
Console.WriteLine($"Property changed on {sender}: {e.PropertyName}");
};
← 목록 - .NET SDK