버전 10.6.0의 새로운 기능.
RealmAny 데이터 유형을 사용하여 여러 기본 유형 중 하나를 포함할 수 있는 Realm 객체 필드를 만들 수 있습니다. RealmList, RealmDictionary 또는 RealmSet 필드에 여러 RealmAny 인스턴스를 저장할 수 있습니다. RealmAny 필드의 값을 변경하려면 다른 기본 값으로 새 RealmAny 인스턴스를 할당합니다. Atlas App Services 백엔드 객체 스키마 에서는 RealmAny 데이터 유형을 혼합 이라고 합니다. RealmAny 필드는 인덱싱이 가능하지만 기본 키로 사용할 수 없습니다.
참고
RealmAny 유형 호환성
RealmAny 객체는 다음을 제외하고 지원되는 모든 필드 유형 을 참조할 수 있습니다.
RealmAnyRealmListRealmSetRealmDictionary
사용법
RealmAny 인스턴스를 만들려면 RealmAny.valueOf() 메서드를 사용하여 초기값을 할당하거나 RealmAny.nullValue() 를 사용하여 값을 할당하지 않습니다. RealmAny 인스턴스는 String 또는 Integer 인스턴스와 마찬가지로 변경되지 않습니다. RealmAny 필드에 새 값을 할당하려면 새 RealmAny 인스턴스를 만들어야 합니다.
경고
두 가지 가능한 null RealmAny 값
RealmAny 인스턴스는 항상 null 을 허용합니다. 또한 인스턴스에는 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 필드를 쿼리할 수 있습니다. 문자열 연산자 및 산술 연산자와 같이 특정 유형에서만 작동하는 연산자는 해당 유형이 포함되지 않은 값을 무시합니다. 이러한 연산자를 부정하면 해당 유형이 포함되지 않은 값과 일치합니다. 유형 쿼리는 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") }