Class MutableRealmInteger
On this page
io.realm
Implemented interfaces:
A MutableRealmInteger
is a mutable, Long -like numeric quantity. It behaves almost exactly as a reference to a Long . More specifically:
A
MutableRealmInteger
may have the valuenull
.The equals operator compares the contained Long values.
null
-valuedMutableRealmInteger
are.equals
The compareTo operator compares the contained Long values. It considers
null
< any non-null
value.The increment and decrement operators throw IllegalStateException when applied to a
null
-valuedMutableRealmInteger
.
MutableRealmInteger
s are most interesting as members of a managed RealmModel object. When managed, the increment and decrement operators implement a
conflict free replicated data type : Simultaneous increments and decrements from multiple distributed clients will be aggregated correctly. For instance, if the value of counter
field for the object representing user "Fred" is currently 0, then the following code, executed on two different devices, simultaneously, even if connected by only a slow, unreliable network, will
always cause the value of counter
to converge, eventually on the value 2.
MutableRealmInteger counter = realm.where(Users.class) .equalTo("name", Fred) .findFirst() .counter.increment(1);
Note that the set(Long) operator must be used with extreme care. It will quash the effects of any prior calls to increment(long) or decrement(long) . Although the value of a MutableRealmInteger
will always converge across devices, the specific value on which it converges will depend on the actual order in which operations took place. Mixing set(Long) with increment(long) and decrement(long) is, therefore, not advised, unless fuzzy counting is acceptable.
MutableRealmInteger
s may not be primary keys. Their implementations are not thread safe. Like all managed Realm objects, managed MutableRealmInteger
s may not be moved across threads. Unmanaged MutableRealmInteger
s may be moved across threads but require safe publication.
A MutableRealmInteger
, in a model class, must always be declared final
. For instance:
public final MutableRealmInteger counter = MutableRealmInteger.ofNull();
Although initializing the MutableRealmInteger
as null
may work very limited circumstances, developers are advised
not to do it:
public final MutableRealmInteger counter = null; // DO NOT DO THIS!
Also note that when a MutableRealmInteger
is @Required
, it is better, though not required, to initialize it with a non-null value.
@Required public final MutableRealmInteger counter = MutableRealmInteger.valueOf(0L);
A reference to a managed MutableRealmInteger
is subject to all of the constraints that apply to the model object from which it was obtained: It can only be mutated within a transaction and it becomes invalid if the Realm backing it is closed. Use the isManaged()
and isValid()
operators to determine whether a MutableRealmInteger
is in a consistent state. Note, in particular, that a reference to a managed MutableRealmInteger
retains a reference to the model object to which it belongs. For example in this code:
MutableRealmInteger counter = realm.where(Users.class).findFirst().counter;
the counter
holds a reference to the User
model object from which it was obtained. Neither can be GCed until all references to both are unreachable.
Method Summary
Modifier and Type | Method and Description |
---|---|
public final int |
|
public abstract void | Decrements the |
public final boolean | |
public abstract Long | get () Gets the |
public final int | hashCode () A |
public abstract void | Increments the |
public final boolean | isNull () |
public static MutableRealmInteger | ofNull () Creates a new, unmanaged |
public final void | Sets the |
public abstract void | |
public static MutableRealmInteger | |
public static MutableRealmInteger | Creates a new, unmanaged |
public static MutableRealmInteger |
Inherited Methods
Methods inherited from class java.lang.Object :
getClass
,hashCode
,equals
,clone
,toString
,notify
,notifyAll
,wait
,wait
,wait
,finalize
Method Detail
compareTo
Parameters
Returns -1, 0, or 1, depending on whether this object's value is <, =, or > the target's. |
decrement
Decrements the Parameters
|
equals
get
hashCode
increment
Increments the Parameters
|
isNull
ofNull
public static MutableRealmInteger ofNull () |
---|
Creates a new, unmanaged MutableRealmInteger whose value is null . |
set
valueOf
Creates a new, unmanaged Parameters
|
Creates a new, unmanaged Parameters
|
Creates a new, unmanaged Parameters
|