FirebaseFirestore Framework Reference

WriteBatch

classWriteBatch:NSObject

A write batch is used to perform multiple writes as a single atomic unit.

A WriteBatch object can be acquired by callingFirestore.batch(). It provides methods for adding writes to the write batch. None of the writes will be committed (or visible locally) untilWriteBatch.commit() is called.

Unlike transactions, write batches are persisted offline and therefore are preferable when you don’t need to condition your writes on read data.

  • Writes to the document referred to bydocument. If the document doesn’t yet exist, this method creates it and then sets the data. If the document exists, this method overwrites the document data with the new values.

    Declaration

    Swift

    funcsetData(_data:[String:Any],forDocumentdocument:FIRDocumentReference)->WriteBatch

    Parameters

    data

    ADictionary that contains the fields and data to write to the document.

    document

    A reference to the document whose data should be overwritten.

    Return Value

    ThisWriteBatch instance. Used for chaining method calls.

  • Writes to the document referred to bydocument. If the document doesn’t yet exist, this method creates it and then sets the data. If you passmerge:true, the provided data will be merged into any existing document.

    Declaration

    Swift

    funcsetData(_data:[String:Any],forDocumentdocument:FIRDocumentReference,merge:Bool)->WriteBatch

    Parameters

    data

    ADictionary that contains the fields and data to write to the document.

    document

    A reference to the document whose data should be overwritten.

    merge

    Whether to merge the provided data into any existing document. If enabled,all omitted fields remain untouched. If your input sets any field to an empty dictionary, anynested field is overwritten.

    Return Value

    ThisWriteBatch instance. Used for chaining method calls.

  • Writes to the document referred to bydocument and only replace the fields specified undermergeFields. Any field that is not specified inmergeFields is ignored and remains untouched. If the document doesn’t yet exist, this method creates it and then sets the data.

    It is an error to include a field inmergeFields that does not have a corresponding value in thedata dictionary.

    Declaration

    Swift

    funcsetData(_data:[String:Any],forDocumentdocument:FIRDocumentReference,mergeFields:[Any])->WriteBatch

    Parameters

    data

    ADictionary that contains the fields and data to write to the document.

    document

    A reference to the document whose data should be overwritten.

    mergeFields

    AnArray that contains a list ofString orFieldPath elementsspecifying which fields to merge. Fields can contain dots to reference nested fields withinthe document. If your input sets any field to an empty dictionary, any nested field isoverwritten.

    Return Value

    ThisWriteBatch instance. Used for chaining method calls.

  • Updates fields in the document referred to bydocument. If document does not exist, the write batch will fail.

    Declaration

    Swift

    funcupdateData(_fields:[AnyHashable:Any],forDocumentdocument:FIRDocumentReference)->WriteBatch

    Parameters

    fields

    ADictionary containing the fields (expressed as anString orFieldPath) and values with which to update the document.

    document

    A reference to the document whose data should be updated.

    Return Value

    ThisWriteBatch instance. Used for chaining method calls.

  • Deletes the document referred to bydocument.

    Declaration

    Swift

    funcdeleteDocument(_document:FIRDocumentReference)->WriteBatch

    Parameters

    document

    A reference to the document that should be deleted.

    Return Value

    ThisWriteBatch instance. Used for chaining method calls.

  • Commits all of the writes in this write batch as a single atomic unit.

    Declaration

    Swift

    funccommit()
  • Commits all of the writes in this write batch as a single atomic unit.

    Declaration

    Swift

    funccommit()asyncthrows

    Parameters

    completion

    A block to be called once all of the writes in the batch have been successfully written to the backend as an atomic unit. This block will only execute when the client is online and the commit has completed against the server. The completion handler will not be called when the device is offline, though local changes will be visible immediately.

  • Encodes an instance ofEncodable and overwrites the encoded datato the document referred bydoc. If no document exists,it is created. If a document already exists, it is overwritten.

    SeeFirestore.Encoder for more details about the encoding process.

    Declaration

    Swift

    @discardableResultfuncsetData<T:Encodable>(fromvalue:T,forDocumentdoc:DocumentReference,encoder:Firestore.Encoder=Firestore.Encoder())throws->WriteBatch

    Parameters

    value

    An instance ofEncodable to be encoded to a document.

    encoder

    The encoder instance to use to run the encoding.

    doc

    The document to create/overwrite the encoded data to.

    Return Value

    This instance ofWriteBatch. Used for chaining method calls.

  • Encodes an instance ofEncodable and overwrites the encoded datato the document referred bydoc. If no document exists,it is created. If a document already exists, it is overwritten. If you passmerge:true, the providedEncodable will be merged into any existing document.

    SeeFirestore.Encoder for more details about the encoding process.

    Declaration

    Swift

    @discardableResultfuncsetData<T:Encodable>(fromvalue:T,forDocumentdoc:DocumentReference,merge:Bool,encoder:Firestore.Encoder=Firestore.Encoder())throws->WriteBatch

    Parameters

    value

    An instance ofEncodable to be encoded to a document.

    doc

    The document to create/overwrite the encoded data to.

    merge

    Whether to merge the providedEncodable into any existing document.

    encoder

    The encoder instance to use to run the encoding.

    Return Value

    This instance ofWriteBatch. Used for chaining method calls.

  • Encodes an instance ofEncodable and writes the encoded data to the document referredbydoc by only replacing the fields specified undermergeFields.Any field that is not specified in mergeFields is ignored and remains untouched. If thedocument doesn’t yet exist, this method creates it and then sets the data.

    It is an error to include a field inmergeFields that does not have a correspondingfield in theEncodable.

    SeeFirestore.Encoder for more details about the encoding process.

    Declaration

    Swift

    @discardableResultfuncsetData<T:Encodable>(fromvalue:T,forDocumentdoc:DocumentReference,mergeFields:[Any],encoder:Firestore.Encoder=Firestore.Encoder())throws->WriteBatch

    Parameters

    value

    An instance ofEncodable to be encoded to a document.

    doc

    The document to create/overwrite the encoded data to.

    mergeFields

    Array ofString orFieldPath elements specifying which fields to merge. Fields can contain dots to reference nested fields within the document.

    encoder

    The encoder instance to use to run the encoding.

    Return Value

    This instance ofWriteBatch. Used for chaining method calls.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-03-11 UTC.