A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/atlas/device-sdks/sdk/java/model-data/data-types/realmany/ below:

RealmAny - Java SDK - Atlas Device SDKs

New in version 10.6.0.

Note New Java SDK apps cannot use RealmAny

New App Services Apps will not be able to synchronize data models with properties of type RealmAny.

You can use the RealmAny data type to create Realm object fields that can contain any of several underlying types. You can store multiple RealmAny instances in RealmList, RealmDictionary, or RealmSet fields. To change the value of a RealmAny field, assign a new RealmAny instance with a different underlying value. In Atlas App Services backend object schemas, the RealmAny data type is called mixed. RealmAny fields are indexable, but cannot be used as primary keys.

Note RealmAny Type Compatiblility

RealmAny objects can refer to any supported field type except:

To create a RealmAny instance, use the RealmAny.valueOf() method to assign an initial value or RealmAny.nullValue() to assign no value. RealmAny instances are immutable just like String or Integer instances; if you want to assign a new value to a RealmAny field, you must create a new RealmAny instance.

Warning Two Possible Null RealmAny Values

RealmAny instances are always nullable. Additionally, instances can contain a value of type 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;        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");    frog.setBestFriend(RealmAny.nullValue());  Log.v("EXAMPLE", "Best friend: " + frog.bestFriendToString());    Assert.assertTrue(frog.getBestFriend().getType() == RealmAny.Type.NULL);    frog.setBestFriend(RealmAny.valueOf("Greg"));  Log.v("EXAMPLE", "Best friend: " + frog.bestFriendToString());    Assert.assertTrue(frog.getBestFriend().getType() == RealmAny.Type.STRING);    Person person = new Person("Jason Funderburker");  frog.setBestFriend(RealmAny.valueOf(person));  Log.v("EXAMPLE", "Best friend: " + frog.bestFriendToString());    Person bestFriendObject = frog.getBestFriend().asRealmModel(Person.class);  Log.v("EXAMPLE", "Best friend: " + bestFriendObject.getName());    Assert.assertTrue(frog.getBestFriend().getType() == RealmAny.Type.OBJECT);      GroupOfPeople persons = new GroupOfPeople();    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.RealmAnyimport io.realm.RealmObjectopen 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"frog.bestFriend = RealmAny.nullValue()Log.v("EXAMPLE", "Best friend: " + frog.bestFriendToString())Assert.assertEquals(frog.bestFriend?.type, RealmAny.Type.NULL)frog.bestFriend = RealmAny.valueOf("Greg")Log.v("EXAMPLE", "Best friend: " + frog.bestFriendToString())Assert.assertEquals(frog.bestFriend?.type, RealmAny.Type.STRING)val person = Person("Jason Funderburker")frog.bestFriend = RealmAny.valueOf(person)Log.v("EXAMPLE", "Best friend: " + frog.bestFriendToString())val bestFriendObject = frog.bestFriend?.asRealmModel(Person::class.java)Log.v("EXAMPLE", "Best friend: " + bestFriendObject?.name)Assert.assertEquals(frog.bestFriend?.type, RealmAny.Type.OBJECT)val persons = GroupOfPeople()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())

You can query a RealmAny field just like any other data type. Operators that only work with certain types, such as string operators and arithmetic operators, ignore values that do not contain that type. Negating such operators matches values that do not contain the type. Type queries match the underlying type, rather than RealmAny. Arithmetic operators convert numeric values implicitly to compare across types.

To subscribe to changes to a RealmAny field, use the RealmObject.addChangeListener method of the enclosing object. You can use the ObjectChangeSet parameter to determine if the RealmAny field changed.

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 -> {        frog.get().setBestFriend(RealmAny.nullValue());    Log.v("EXAMPLE", "Best friend: " + frog.get().bestFriendToString());        frog.get().setBestFriend(RealmAny.valueOf("Greg"));});
var frog: Frog? = nullrealm.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? ->        frog?.bestFriend = RealmAny.nullValue()    Log.v("EXAMPLE", "Best friend: " + frog?.bestFriendToString())        frog?.bestFriend = RealmAny.valueOf("Greg")}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4