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
child(path: String)

Used to obtain a MutableData instance that encapsulates the data and priority at the given relative path.

Boolean
equals(o: Any!)
(Mutable)Iterable<MutableData!>

Used to iterate over the immediate children at this location

Long
String?
Any?

Gets the current priority at this location.

Any?

getValue() returns the data contained in this instance as native types.

T?

Due to the way that Java implements generics, it takes an extra step to get back a properly-typed Collection.

T?
<T> getValue(valueType: Class<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
setValue(value: Any?)

Set the data at this location to the given value.

String!

Extension functions

inline T?

Returns the content of the MutableData converted to a POJO.

inline T?

This function is deprecated. Migrate to use the KTX API from the main module: https://firebase.google.com/docs/android/kotlin-migration.

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

equals

fun equals(o: Any!): Boolean

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:

  • Double
  • String
Note that null is allowed
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:

  • Boolean
  • Long
  • Double
  • String
  • Map<String, Object>
  • List<Object>
This list is recursive; the possible types for 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);
It is important to use a subclass of GenericTypeIndicator. See GenericTypeIndicator for more details
Parameters
<T>

The type to return. Implicitly defined from the GenericTypeIndicator passed in

t: GenericTypeIndicator<T!>

A subclass of GenericTypeIndicator indicating the type of generic collection to be returned.

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:

  1. The class must have a default constructor that takes no arguments
  2. 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
An example class might look like:
    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:

  • Boolean
  • Long
  • Double
  • Map<String, Object>
  • List<Object>
In addition, you can set instances of your own class into this location, provided they satisfy the following constraints:
  1. The class must have a default constructor that takes no arguments
  2. 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
Generic collections of objects that satisfy the above constraints are also permitted, i.e. 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

toString

fun toString(): String!

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.

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.

Deprecation Notice: The Kotlin extensions (KTX) APIs have been added to their respective main modules, and the Kotlin extension (KTX) APIs in com.google.firebase.firebase-database-ktx are now deprecated. As early as April 2024, we'll no longer release KTX modules. For details, see the FAQ about this initiative.