문서 홈 → 애플리케이션 개발 → Atlas Device SDK
RealmInteger - .NET SDK
개요
Realm은 논리적 카운터로 사용할 수 있는 특수 정수 유형으로 RealmInteger 를 제공합니다. RealmInteger<T>
는 동기화된 Realm을 사용할 때 의도를 더 명확하게 표현하고 더 나은 충돌 해결 단계를 생성할 수 있는 추가 API를 노출합니다. 유형 인수 <T>
의 유형은 byte
, short
, int
또는 long
수 있습니다. 다음 예는 int
에 매핑되는 RealmInteger
속성을 사용하는 방법을 보여줍니다.
public partial class MyRealmClass : IRealmObject { [ ] public int _id { get; set; } public RealmInteger<int> Counter { get; set; } }
카운터 구현
일반적으로 값을 읽고 증가시킨 다음 설정하여(myObject.Counter += 1
) 카운터를 구현합니다. 이는 두 클라이언트가 오프라인 상태와 같은 비동기 상황에서는 잘 작동하지 않습니다. 다음 시나리오를 가정해 보겠습니다.
Realm 객체에
int
유형의counter
속성이 있습니다. 현재10
값으로 설정되어 있습니다.클라이언트 1과 클라이언트 2는 모두
counter
속성(10
)을 읽고 각각 값을1
씩 증가시킵니다.각 클라이언트가 연결을 회복하고 변경 사항을 병합할 때 예상되는 값은 11이며 충돌은 없습니다. 그러나 카운터 값은
12
이어야 합니다!
RealmInteger
그러나 을 Increment()
사용하는 Decrement()
경우 및 메서드를 호출하고 카운터를 재설정하려면 과 마찬가지로 0
로 int
설정합니다.
var myObject = realm.Find<MyRealmClass>(id); // myObject.Counter == 0 realm.Write(() => { // Increment the value of the RealmInteger myObject.Counter.Increment(); // 1 myObject.Counter.Increment(5); // 6 // Decrement the value of the RealmInteger // Note the use of Increment with a negative number myObject.Counter.Decrement(); // 5 myObject.Counter.Increment(-3); // 2 // Reset the RealmInteger myObject.Counter = 0; // RealmInteger<T> is implicitly convertable to T: int bar = myObject.Counter; });
중요
RealmInteger
을(를) 재설정할 때 위에서 설명한 오프라인 병합 문제가 발생할 수 있습니다.
RealmInteger
는 기존 integer
유형으로 지원되므로 속성 유형을 T
에서 RealmInteger<T>
로 변경할 때 스키마를 마이그레이션할 필요가 없습니다.