Atlas Device SDK 已弃用。 有关详细信息,请参阅弃用页面。
版本 10.6.0 中的新增内容。
您可以使用RealmAny数据类型创建 Realm 对象字段,其中可以包含多种基础类型。 您可以在RealmList 、 RealmDictionary或RealmSet字段中存储多个RealmAny实例。 要更改RealmAny字段的值,请为新的RealmAny实例分配不同的基础值。 在 Atlas App Services 后端对象模式中, RealmAny数据类型称为“混合”。 RealmAny字段可索引,但不能用作主键。
使用
要创建RealmAny实例,请使用RealmAny.valueOf() 方法分配初始值,或使用RealmAny.nullValue()不分配任何值。 RealmAny实例不可变,就像String或Integer实例一样;如果要为RealmAny字段分配新值,则必须创建新的RealmAny实例。
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字段。 仅适用于某些类型的操作符(例如字符串操作符和算术操作符)会忽略不包含该类型的值。 否定此类操作符会匹配不包含该类型的值。 类型查询匹配底层类型,而不是RealmAny 。 算术运算符隐式转换数值以进行跨类型比较。
通知
要订阅对RealmAny字段的更改,请使用RealmObject.addChangeListener 封闭对象的方法。 您可以使用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>() { public void onChange( Frog frog, 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") }