Transaction

public 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

public interface Transaction.Function<TResult>

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

Public methods

@NonNull Transaction

Deletes the document referred to by the provided DocumentReference.

@NonNull DocumentSnapshot

Reads the document referenced by this DocumentReference

@NonNull Transaction
set(@NonNull DocumentReference documentRef, @NonNull Object data)

Overwrites the document referred to by the provided DocumentReference.

@NonNull Transaction
set(
    @NonNull DocumentReference documentRef,
    @NonNull Object data,
    @NonNull SetOptions options
)

Writes to the document referred to by the provided DocumentReference.

@NonNull Transaction
update(
    @NonNull DocumentReference documentRef,
    @NonNull Map<StringObject> data
)

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

@NonNull Transaction
update(
    @NonNull DocumentReference documentRef,
    @NonNull String field,
    @Nullable Object value,
    Object[] moreFieldsAndValues
)

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

@NonNull Transaction
update(
    @NonNull DocumentReference documentRef,
    @NonNull FieldPath fieldPath,
    @Nullable Object value,
    Object[] moreFieldsAndValues
)

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

Public methods

delete

public @NonNull Transaction delete(@NonNull DocumentReference documentRef)

Deletes the document referred to by the provided DocumentReference.

Parameters
@NonNull DocumentReference documentRef

The DocumentReference to delete.

Returns
@NonNull Transaction

This Transaction instance. Used for chaining method calls.

get

public @NonNull DocumentSnapshot get(@NonNull DocumentReference documentRef)

Reads the document referenced by this DocumentReference

Parameters
@NonNull DocumentReference documentRef

The DocumentReference to read.

Returns
@NonNull DocumentSnapshot

The contents of the Document at this DocumentReference.

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

set

public @NonNull Transaction set(@NonNull DocumentReference documentRef, @NonNull Object data)

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
@NonNull DocumentReference documentRef

The DocumentReference to overwrite.

@NonNull Object data

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

Returns
@NonNull Transaction

This Transaction instance. Used for chaining method calls.

set

public @NonNull Transaction set(
    @NonNull DocumentReference documentRef,
    @NonNull Object data,
    @NonNull SetOptions options
)

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
@NonNull DocumentReference documentRef

The DocumentReference to overwrite.

@NonNull Object data

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

@NonNull SetOptions options

An object to configure the set behavior.

Returns
@NonNull Transaction

This Transaction instance. Used for chaining method calls.

update

public @NonNull Transaction update(
    @NonNull DocumentReference documentRef,
    @NonNull Map<StringObject> data
)

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

Parameters
@NonNull DocumentReference documentRef

The DocumentReference to update.

@NonNull Map<StringObject> data

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

Returns
@NonNull Transaction

This Transaction instance. Used for chaining method calls.

update

public @NonNull Transaction update(
    @NonNull DocumentReference documentRef,
    @NonNull String field,
    @Nullable Object value,
    Object[] moreFieldsAndValues
)

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

Parameters
@NonNull DocumentReference documentRef

The DocumentReference to update.

@NonNull String field

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

@Nullable Object value

The first value

Object[] moreFieldsAndValues

Additional field/value pairs.

Returns
@NonNull Transaction

This Transaction instance. Used for chaining method calls.

update

public @NonNull Transaction update(
    @NonNull DocumentReference documentRef,
    @NonNull FieldPath fieldPath,
    @Nullable Object value,
    Object[] moreFieldsAndValues
)

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

Parameters
@NonNull DocumentReference documentRef

The DocumentReference to update.

@NonNull FieldPath fieldPath

The first field to update.

@Nullable Object value

The first value

Object[] moreFieldsAndValues

Additional field/value pairs.

Returns
@NonNull Transaction

This Transaction instance. Used for chaining method calls.