Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
データ型

RealmAny - Java SDK

バージョン10.6.0の新機能

注意

新しい Java SDK アプリでは RealmAny を使用できません

新しい App Services アプリは、 RealmAnyタイプのプロパティを持つデータモデルを同期できません。

RealmAnyデータ型を使用して、複数の基礎となるタイプのいずれかを含めることができる Realm オブジェクト フィールドを作成できます。 複数のRealmAnyインスタンスをRealmListRealmDictionary 、またはRealmSetフィールドに保存できます。 RealmAnyフィールドの値を変更するには、別の基礎値を持つ新しいRealmAnyインスタンスを割り当てます。 Atlas App Services バックエンドオブジェクト スキーマでは、 RealmAnyデータ型は混合と呼ばれます。 RealmAnyフィールドはインデックス作成可能ですが、プライマリキーとして使用することはできません。

注意

RealmAny 型の互換性

RealmAny オブジェクトは、サポートされている任意のフィールドタイプを除く)を参照できます。

  • RealmAny

  • RealmList

  • RealmSet

  • RealmDictionary

RealmAnyインスタンスを作成するには、 RealmAny.valueOf() メソッドを使用して初期値を割り当てるか、 RealmAny.nullValue()を使用して値を割り当てません。 RealmAnyStringIntegerインスタンスは、 インスタンスまたはRealmAny RealmAnyインスタンスと同様に不変です。 フィールドに新しい値を割り当てる場合は、新しい インスタンスを作成する必要があります。

警告

2 つの null RealmAny 値

RealmAny インスタンスは常にnullableです。 さらに、 インスタンスにはRealmAny.Type.NULL型の値を含めることができます。

import com.mongodb.realm.examples.model.kotlin.Person;
import io.realm.RealmAny;
import io.realm.RealmObject;
public class Frog extends RealmObject {
String name;
RealmAny bestFriend;
// realm-required empty constructor
public Frog() {}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public RealmAny getBestFriend() { return bestFriend; }
public void setBestFriend(RealmAny bestFriend) { this.bestFriend = bestFriend; }
public String bestFriendToString() {
switch(bestFriend.getType()) {
case NULL: {
return "no best friend";
}
case STRING: {
return bestFriend.asString();
}
case OBJECT: {
if (bestFriend.getValueClass().equals(Person.class)) {
Person person = bestFriend.asRealmModel(Person.class);
return person.getName();
}
}
default: {
return "unknown type";
}
}
}
}
Frog frog = realm.createObject(Frog.class);
frog.setName("Jonathan Livingston Applesauce");
// set RealmAny field to a null value
frog.setBestFriend(RealmAny.nullValue());
Log.v("EXAMPLE", "Best friend: " + frog.bestFriendToString());
// possible types for RealmAny are defined in RealmAny.Type
Assert.assertTrue(frog.getBestFriend().getType() == RealmAny.Type.NULL);
// set RealmAny field to a string with RealmAny.valueOf a string value
frog.setBestFriend(RealmAny.valueOf("Greg"));
Log.v("EXAMPLE", "Best friend: " + frog.bestFriendToString());
// RealmAny instances change type as you reassign to different values
Assert.assertTrue(frog.getBestFriend().getType() == RealmAny.Type.STRING);
// set RealmAny field to a realm object, also with valueOf
Person person = new Person("Jason Funderburker");
frog.setBestFriend(RealmAny.valueOf(person));
Log.v("EXAMPLE", "Best friend: " + frog.bestFriendToString());
// You can also extract underlying Realm Objects from RealmAny with asRealmModel
Person bestFriendObject = frog.getBestFriend().asRealmModel(Person.class);
Log.v("EXAMPLE", "Best friend: " + bestFriendObject.getName());
// RealmAny fields referring to any Realm Object use the OBJECT type
Assert.assertTrue(frog.getBestFriend().getType() == RealmAny.Type.OBJECT);
// you can't put a RealmList in a RealmAny field directly,
// ...but you can set a RealmAny field to a RealmObject that contains a list
GroupOfPeople persons = new GroupOfPeople();
// GroupOfPeople contains a RealmList of people
persons.getPeople().add("Rand");
persons.getPeople().add("Perrin");
persons.getPeople().add("Mat");
frog.setBestFriend(RealmAny.valueOf(persons));
Log.v("EXAMPLE", "Best friend: " +
frog.getBestFriend().asRealmModel(GroupOfPeople.class).getPeople().toString());
import io.realm.RealmAny
import io.realm.RealmObject
open class Frog(var bestFriend: RealmAny? = RealmAny.nullValue()) : RealmObject() {
var name: String? = null
open fun bestFriendToString(): String {
if (bestFriend == null) {
return "null"
}
return when (bestFriend!!.type) {
RealmAny.Type.NULL -> {
"no best friend"
}
RealmAny.Type.STRING -> {
bestFriend!!.asString()
}
RealmAny.Type.OBJECT -> {
if (bestFriend!!.valueClass == Person::class.java) {
val person = bestFriend!!.asRealmModel(Person::class.java)
person.name
}
"unknown type"
}
else -> {
"unknown type"
}
}
}
}
val frog = realm.createObject(Frog::class.java)
frog.name = "George Washington"
// set RealmAny field to a null value
// set RealmAny field to a null value
frog.bestFriend = RealmAny.nullValue()
Log.v("EXAMPLE", "Best friend: " + frog.bestFriendToString())
// possible types for RealmAny are defined in RealmAny.Type
Assert.assertEquals(frog.bestFriend?.type, RealmAny.Type.NULL)
// set RealmAny field to a string with RealmAny.valueOf a string value
frog.bestFriend = RealmAny.valueOf("Greg")
Log.v("EXAMPLE", "Best friend: " + frog.bestFriendToString())
// RealmAny instances change type as you reassign to different values
Assert.assertEquals(frog.bestFriend?.type, RealmAny.Type.STRING)
// set RealmAny field to a realm object, also with valueOf
val person = Person("Jason Funderburker")
frog.bestFriend = RealmAny.valueOf(person)
Log.v("EXAMPLE", "Best friend: " + frog.bestFriendToString())
// You can also extract underlying Realm Objects from RealmAny with asRealmModel
val bestFriendObject = frog.bestFriend?.asRealmModel(Person::class.java)
Log.v("EXAMPLE", "Best friend: " + bestFriendObject?.name)
// RealmAny fields referring to any Realm Object use the OBJECT type
Assert.assertEquals(frog.bestFriend?.type, RealmAny.Type.OBJECT)
// you can't put a RealmList in a RealmAny field directly,
// ...but you can set a RealmAny field to a RealmObject that contains a list
val persons = GroupOfPeople()
// GroupOfPeople contains a RealmList of people
persons.people.add("Rand")
persons.people.add("Perrin")
persons.people.add("Mat")
frog.bestFriend = RealmAny.valueOf(persons)
Log.v("EXAMPLE", "Best friend: " +
frog.bestFriend?.asRealmModel(GroupOfPeople::class.java)
?.people.toString())

RealmAnyフィールドは他のデータ型と同様にクエリできます。 string演算子や算術演算子など、特定のタイプでのみ機能する演算子は、そのタイプを含まない値を無視します。 このような演算子を否定すると、 型を含まない値と一致します。 タイプ クエリがRealmAnyではなく、基礎のタイプと一致する 算術演算子は暗黙的に数値を変換し、型間で比較します。

RealmAnyフィールドへの変更をサブスクライブするには、 RealmObject.addCheckListsを使用します 囲むオブジェクトのメソッドです。 ObjectchangeSetパラメータを使用して、 RealmAnyフィールドが変更されたかどうかを判断できます。

AtomicReference<Frog> frog = new AtomicReference<Frog>();
realm.executeTransaction(r -> {
frog.set(realm.createObject(Frog.class));
frog.get().setName("Jonathan Livingston Applesauce");
});
RealmObjectChangeListener<Frog> objectChangeListener =
new RealmObjectChangeListener<Frog>() {
@Override
public void onChange(@NotNull Frog frog, @Nullable ObjectChangeSet changeSet) {
if (changeSet != null) {
Log.v("EXAMPLE", "Changes to fields: " +
Arrays.toString(changeSet.getChangedFields()));
if (changeSet.isFieldChanged("best_friend")) {
Log.v("EXAMPLE", "RealmAny best friend field changed to : " +
frog.bestFriendToString());
}
}
}
};
frog.get().addChangeListener(objectChangeListener);
realm.executeTransaction(r -> {
// set RealmAny field to a null value
frog.get().setBestFriend(RealmAny.nullValue());
Log.v("EXAMPLE", "Best friend: " + frog.get().bestFriendToString());
// set RealmAny field to a string with RealmAny.valueOf a string value
frog.get().setBestFriend(RealmAny.valueOf("Greg"));
});
var frog: Frog? = null
realm.executeTransaction { r: Realm? ->
frog = realm.createObject(Frog::class.java)
frog?.name = "Jonathan Livingston Applesauce"
}
val objectChangeListener
= RealmObjectChangeListener<Frog> { frog, changeSet ->
if (changeSet != null) {
Log.v("EXAMPLE", "Changes to fields: " +
changeSet.changedFields)
if (changeSet.isFieldChanged("best_friend")) {
Log.v("EXAMPLE", "RealmAny best friend field changed to : " +
frog.bestFriendToString())
}
}
}
frog?.addChangeListener(objectChangeListener)
realm.executeTransaction { r: Realm? ->
// set RealmAny field to a null value
frog?.bestFriend = RealmAny.nullValue()
Log.v("EXAMPLE", "Best friend: " + frog?.bestFriendToString())
// set RealmAny field to a string with RealmAny.valueOf a string value
frog?.bestFriend = RealmAny.valueOf("Greg")
}

戻る

セット

項目一覧