Docs Menu
Docs Home
/ /
サポートされているデータ型

RealmInteger - .NET SDK

Realm では、論理カウンターとして使用できる特別な整数型として RealmIntegerが提供されています。 {4RealmInteger<T> は、同期済み RealmAPI を使用するときに、意向をより明確に し、より優れた競合解決ステップを生成できる追加の を公開します。Express型引数<T>は、 byteshortint 、またはlongのいずれかになります。 次の例では、 intにマッピングするRealmIntegerプロパティを使用する方法を示しています。

public partial class MyRealmClass : IRealmObject
{
[PrimaryKey]
public int _id { get; set; }
public RealmInteger<int> Counter { get; set; }
}

従来は、値を読み取って増加させてから設定することでカウンターを実装します( myObject.Counter += 1 )。 これは、2 つのクライアントがオフラインの場合などの非同期状況では機能しません。 次のシナリオを検討してみましょう。

  • Realm オブジェクトには、タイプintcounterプロパティがあります。 現在、 10の値に設定されています。

  • クライアント 1 と 2 は両方ともcounterプロパティ( 10 )を読み取り、それぞれの値を1ずつ増加させます。

  • 各クライアントが接続を回復し、変更をマージすると、値は 11 になることが予想され、競合は発生しません。 ただし、カウンター値は12である必要があります。

RealmIntegerIncrement()ただし、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>に変更するときにスキーマの移行は必要ありません。

戻る

RealmValue

項目一覧