Transaction

class Transaction


A Transaction is passed to a Function to provide the methods to read and write data within the transaction context.

Subclassing Note: Cloud Firestore classes are not meant to be subclassed except for use in test mocks. Subclassing is not supported in production code and new SDK releases may break code that does so.

See also
runTransaction

Summary

Nested types

interface Transaction.Function<TResult>

An interface for providing code to be executed within a transaction context.

Public functions

Transaction
delete(documentRef: DocumentReference)

Deletes the document referred to by the provided DocumentReference.

DocumentSnapshot
get(documentRef: DocumentReference)

Reads the document referenced by this DocumentReference

Transaction
set(documentRef: DocumentReference, data: Any)

Overwrites the document referred to by the provided DocumentReference.

Transaction
set(documentRef: DocumentReference, data: Any, options: SetOptions)

Writes to the document referred to by the provided DocumentReference.

Transaction
update(documentRef: DocumentReference, data: (Mutable)Map<String!, Any!>)

Updates fields in the document referred to by the provided DocumentReference.

Transaction
update(
    documentRef: DocumentReference,
    field: String,
    value: Any?,
    moreFieldsAndValues: Array<Any!>!
)

Updates fields in the document referred to by the provided DocumentReference.

Transaction
update(
    documentRef: DocumentReference,
    fieldPath: FieldPath,
    value: Any?,
    moreFieldsAndValues: Array<Any!>!
)

Updates fields in the document referred to by the provided DocumentReference.

Public functions

delete

fun delete(documentRef: DocumentReference): Transaction

Deletes the document referred to by the provided DocumentReference.

Parameters
documentRef: DocumentReference

The DocumentReference to delete.

Returns
Transaction

This Transaction instance. Used for chaining method calls.

get

fun get(documentRef: DocumentReference): DocumentSnapshot

Reads the document referenced by this DocumentReference

Parameters
documentRef: DocumentReference

The DocumentReference to read.

Returns
DocumentSnapshot

The contents of the Document at this DocumentReference.

Throws
com.google.firebase.firestore.FirebaseFirestoreException: com.google.firebase.firestore.FirebaseFirestoreException

set

fun set(documentRef: DocumentReference, data: Any): Transaction

Overwrites the document referred to by the provided DocumentReference. If the document does not yet exist, it will be created. If a document already exists, it will be overwritten.

Parameters
documentRef: DocumentReference

The DocumentReference to overwrite.

data: Any

The data to write to the document (like a Map or a POJO containing the desired document contents).

Returns
Transaction

This Transaction instance. Used for chaining method calls.

set

fun set(documentRef: DocumentReference, data: Any, options: SetOptions): Transaction

Writes to the document referred to by the provided DocumentReference. If the document does not yet exist, it will be created. If you pass SetOptions, the provided data can be merged into an existing document.

Parameters
documentRef: DocumentReference

The DocumentReference to overwrite.

data: Any

The data to write to the document (like a Map or a POJO containing the desired document contents).

options: SetOptions

An object to configure the set behavior.

Returns
Transaction

This Transaction instance. Used for chaining method calls.

update

fun update(documentRef: DocumentReference, data: (Mutable)Map<String!, Any!>): Transaction

Updates fields in the document referred to by the provided DocumentReference. If no document exists yet, the update will fail.

Parameters
documentRef: DocumentReference

The DocumentReference to update.

data: (Mutable)Map<String!, Any!>

A map of field / value pairs to update. Fields can contain dots to reference nested fields within the document.

Returns
Transaction

This Transaction instance. Used for chaining method calls.

update

fun update(
    documentRef: DocumentReference,
    field: String,
    value: Any?,
    moreFieldsAndValues: Array<Any!>!
): Transaction

Updates fields in the document referred to by the provided DocumentReference. If no document exists yet, the update will fail.

Parameters
documentRef: DocumentReference

The DocumentReference to update.

field: String

The first field to update. Fields can contain dots to reference a nested field within the document.

value: Any?

The first value

moreFieldsAndValues: Array<Any!>!

Additional field/value pairs.

Returns
Transaction

This Transaction instance. Used for chaining method calls.

update

fun update(
    documentRef: DocumentReference,
    fieldPath: FieldPath,
    value: Any?,
    moreFieldsAndValues: Array<Any!>!
): Transaction

Updates fields in the document referred to by the provided DocumentReference. If no document exists yet, the update will fail.

Parameters
documentRef: DocumentReference

The DocumentReference to update.

fieldPath: FieldPath

The first field to update.

value: Any?

The first value

moreFieldsAndValues: Array<Any!>!

Additional field/value pairs.

Returns
Transaction

This Transaction instance. Used for chaining method calls.