Transaction
class Transaction : NSObject
Transaction
provides methods to read and write data within a transaction.
-
Writes to the document referred to by
document
. 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
func setData(_ data: [String : Any], forDocument document: FIRDocumentReference) -> Transaction
Parameters
data
A
Dictionary
that contains the fields and data to write to the document.document
A reference to the document whose data should be overwritten.
Return Value
This
Transaction
instance. Used for chaining method calls. -
Writes to the document referred to by
document
. 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
func setData(_ data: [String : Any], forDocument document: FIRDocumentReference, merge: Bool) -> Transaction
Parameters
data
A
Dictionary
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, any nested field is overwritten.
Return Value
This
Transaction
instance. Used for chaining method calls. -
Writes to the document referred to by
document
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 in
mergeFields
that does not have a corresponding value in thedata
dictionary.Declaration
Swift
func setData(_ data: [String : Any], forDocument document: FIRDocumentReference, mergeFields: [Any]) -> Transaction
Parameters
data
A
Dictionary
containing the fields that make up the document to be written.document
A reference to the document whose data should be overwritten.
mergeFields
An
Array
that contains a list ofString
orFieldPath
elements specifying which fields to merge. Fields can contain dots to reference nested fields within the document. If your input sets any field to an empty dictionary, any nested field is overwritten.Return Value
This
Transaction
instance. Used for chaining method calls. -
Updates fields in the document referred to by
document
. If the document does not exist, the transaction will fail.Declaration
Swift
func updateData(_ fields: [AnyHashable : Any], forDocument document: FIRDocumentReference) -> Transaction
Parameters
fields
A
Dictionary
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
This
Transaction
instance. Used for chaining method calls. -
Deletes the document referred to by
document
.Declaration
Swift
func deleteDocument(_ document: FIRDocumentReference) -> Transaction
Parameters
document
A reference to the document that should be deleted.
Return Value
This
Transaction
instance. Used for chaining method calls. -
Reads the document referenced by
document
.Declaration
Swift
func getDocument(_ document: FIRDocumentReference) throws -> FIRDocumentSnapshot
Parameters
document
A reference to the document to be read.
error
An out parameter to capture an error, if one occurred.
-
Encodes an instance of
Encodable
and overwrites the encoded data to the document referred bydoc
. If no document exists, it is created. If a document already exists, it is overwritten.See
Firestore.Encoder
for more details about the encoding process.Declaration
Swift
@discardableResult func setData<T: Encodable>(from value: T, forDocument doc: DocumentReference, encoder: Firestore.Encoder = Firestore .Encoder()) throws -> Transaction
Parameters
value
a instance of
Encoded
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 of
Transaction
. Used for chaining method calls. -
Encodes an instance of
Encodable
and overwrites the encoded data to the document referred bydoc
. If no document exists, it is created. If a document already exists, it is overwritten. If you pass merge:true, the providedEncodable
will be merged into any existing document.See
Firestore.Encoder
for more details about the encoding process.Declaration
Swift
@discardableResult func setData<T: Encodable>(from value: T, forDocument doc: DocumentReference, merge: Bool, encoder: Firestore.Encoder = Firestore .Encoder()) throws -> Transaction
Parameters
value
An instance of
Encodable
to be encoded to a document.doc
The document to create/overwrite the encoded data to.
merge
Whether to merge the provided
Encodable
into any existing document.encoder
The encoder instance to use to run the encoding.
Return Value
This instance of
Transaction
. Used for chaining method calls. -
Encodes an instance of
Encodable
and writes the encoded data to the document referred bydoc
by only replacing the fields specified undermergeFields
. Any field that is not specified in mergeFields 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 in
mergeFields
that does not have a corresponding field in theEncodable
.See
Firestore.Encoder
for more details about the encoding process.Declaration
Swift
@discardableResult func setData<T: Encodable>(from value: T, forDocument doc: DocumentReference, mergeFields: [Any], encoder: Firestore.Encoder = Firestore .Encoder()) throws -> Transaction
Parameters
value
An instance of
Encodable
to be encoded to a document.doc
The document to create/overwrite the encoded data to.
mergeFields
Array of
String
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 of
Transaction
. Used for chaining method calls.