MutableData
class MutableData
Instances of this class encapsulate the data and priority at a location. It is used in transactions, and it is intended to be inspected and then updated to the desired data at that location. Note that changes made to a child MutableData instance will be visible to the parent and vice versa.
Summary
Public functions |
|
|---|---|
MutableData |
Used to obtain a MutableData instance that encapsulates the data and priority at the given relative path. |
Boolean |
|
(Mutable)Iterable<MutableData!> |
Used to iterate over the immediate children at this location |
Long |
|
String? |
getKey() |
Any? |
Gets the current priority at this location. |
Any? |
getValue()getValue() returns the data contained in this instance as native types. |
T? |
<T> getValue(t: GenericTypeIndicator<T!>)Due to the way that Java implements generics, it takes an extra step to get back a properly-typed Collection. |
T? |
This method is used to marshall the data contained in this instance into a class of your choosing. |
Boolean |
|
Boolean |
|
Unit |
setPriority(priority: Any?)Sets the priority at this location |
Unit |
Set the data at this location to the given value. |
String! |
toString() |
Extension functions |
|
|---|---|
inline T? |
<T : Any?> MutableData.getValue()Returns the content of the MutableData converted to a POJO. |
Public functions
child
fun child(path: String): MutableData
Used to obtain a MutableData instance that encapsulates the data and priority at the given relative path.
| Parameters | |
|---|---|
path: String |
A relative path |
| Returns | |
|---|---|
MutableData |
An instance encapsulating the data and priority at the given path |
getChildren
fun getChildren(): (Mutable)Iterable<MutableData!>
Used to iterate over the immediate children at this location
for (MutableData child : parent.getChildren()) { ... }
| Returns | |
|---|---|
(Mutable)Iterable<MutableData!> |
The immediate children at this location |
getChildrenCount
fun getChildrenCount(): Long
| Returns | |
|---|---|
Long |
The number of immediate children at this location |
getKey
fun getKey(): String?
| Returns | |
|---|---|
String? |
The key name of this location, or null if it is the top-most location |
getPriority
fun getPriority(): Any?
Gets the current priority at this location. The possible return types are:
DoubleString
| Returns | |
|---|---|
Any? |
The priority at this location as a native type or null if no priority was set |
getValue
fun getValue(): Any?
getValue() returns the data contained in this instance as native types. The possible types returned are:
BooleanLongDoubleStringMap<String, Object>List<Object>
java.lang.Object in the above list is given by the same list. These types correspond to the types available in JSON.
| Returns | |
|---|---|
Any? |
The data contained in this instance as native types, or null if there is no data at this location. |
getValue
fun <T> getValue(t: GenericTypeIndicator<T!>): T?
Due to the way that Java implements generics, it takes an extra step to get back a properly-typed Collection. So, in the case where you want a java.util.List of Message instances, you will need to do something like the following:
GenericTypeIndicator<List<Message>> t = new GenericTypeIndicator<List<Message>>() {}; List<Message> messages = mutableData.getValue(t);
GenericTypeIndicator. See GenericTypeIndicator for more details
| Parameters | |
|---|---|
<T> |
The type to return. Implicitly defined from the |
t: GenericTypeIndicator<T!> |
A subclass of |
| Returns | |
|---|---|
T? |
A properly typed collection, populated with the data from this instance, or null if there is no data at this location. |
getValue
fun <T> getValue(valueType: Class<T!>): T?
This method is used to marshall the data contained in this instance into a class of your choosing. The class must fit 2 simple constraints:
- The class must have a default constructor that takes no arguments
- The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized
class Message { private String author; private String text; private Message() {} public Message(String author, String text) { this.author = author; this.text = text; } public String getAuthor() { return author; } public String getText() { return text; } } // Later Message m = mutableData.getValue(Message.class);
| Parameters | |
|---|---|
<T> |
The type to return. Implicitly defined from the class passed in |
valueType: Class<T!> |
The class into which this data in this instance should be marshalled |
| Returns | |
|---|---|
T? |
An instance of the class passed in, populated with the data from this instance, or null if there is no data at this location. |
hasChild
fun hasChild(path: String): Boolean
| Parameters | |
|---|---|
path: String |
A relative path |
| Returns | |
|---|---|
Boolean |
True if data exists at the given path, otherwise false |
hasChildren
fun hasChildren(): Boolean
| Returns | |
|---|---|
Boolean |
True if the data at this location has children, false otherwise |
setPriority
fun setPriority(priority: Any?): Unit
Sets the priority at this location
| Parameters | |
|---|---|
priority: Any? |
The desired priority or null to clear the existing priority |
setValue
fun setValue(value: Any?): Unit
Set the data at this location to the given value. The native types accepted by this method for the value correspond to the JSON types:
BooleanLongDoubleMap<String, Object>List<Object>
- The class must have a default constructor that takes no arguments
- The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized
Map<String, MyPOJO>, as well as null values.
Note that this overrides the priority, which must be set separately.
| Parameters | |
|---|---|
value: Any? |
The value to set at this location or null to delete the existing data |
| Throws | |
|---|---|
com.google.firebase.database.DatabaseException: com.google.firebase.database.DatabaseException |
Extension functions
getValue
inline fun <T : Any?> MutableData.getValue(): T?
Returns the content of the MutableData converted to a POJO.
Supports generics like List<> or Map<>. Use @JvmSuppressWildcards to force the compiler to use the type T, and not ? extends T.