DocumentReference
extension DocumentReference: DocumentIDWrappable-
Declaration
Swift
public static func wrap(_ documentReference: DocumentReference) throws -> Self -
Fetches and decodes the document referenced by this
DocumentReference.This allows users to retrieve a Firestore document and have it decoded to an instance of caller-specified type as follows:
ref.getDocument(as: Book.self) { result in do { let book = try result.get() } catch { // Handle error } }This method attempts to provide up-to-date data when possible by waiting for data from the server, but it may return cached data or fail if you are offline and the server cannot be reached. If
Tdenotes an optional type, the method returns a successful status with a value ofnilfor non-existing documents.Declaration
Swift
func getDocument<T: Decodable>(as type: T.Type, with serverTimestampBehavior: ServerTimestampBehavior = .none, decoder: Firestore.Decoder = .init(), completion: @escaping (Result<T, Error>) -> Void)Parameters
asA
Decodabletype to convert the document fields to.serverTimestampBehaviorConfigures how server timestamps that have not yet been set to their final value are returned from the snapshot.
decoderThe decoder to use to convert the document. Defaults to use the default decoder.
completionThe closure to call when the document snapshot has been fetched and decoded.
-
Fetches and decodes the document referenced by this
DocumentReference.This allows users to retrieve a Firestore document and have it decoded to an instance of caller-specified type as follows:
do { let book = try await ref.getDocument(as: Book.self) } catch { // Handle error }This method attempts to provide up-to-date data when possible by waiting for data from the server, but it may return cached data or fail if you are offline and the server cannot be reached. If
Tdenotes an optional type, the method returns a successful status with a value ofnilfor non-existing documents.Declaration
Swift
@available(iOS 15, tvOS 15, macOS 12, watchOS 8, *) func getDocument<T: Decodable>(as type: T.Type, with serverTimestampBehavior: ServerTimestampBehavior = .none, decoder: Firestore.Decoder = .init()) async throws -> TParameters
asA
Decodabletype to convert the document fields to.serverTimestampBehaviorConfigures how server timestamps that have not yet been set to their final value are returned from the snapshot.
decoderThe decoder to use to convert the document. Defaults to use the default decoder.
Return Value
This instance of the supplied
DecodabletypeT. -
Encodes an instance of
Encodableand overwrites the encoded data to the document referred by thisDocumentReference. If no document exists, it is created. If a document already exists, it is overwritten.See
Firestore.Encoderfor more details about the encoding process.Declaration
Swift
func setData<T: Encodable>(from value: T, encoder: Firestore.Encoder = Firestore.Encoder(), completion: ((Error?) -> Void)? = nil) throwsParameters
valueAn instance of
Encodableto be encoded to a document.encoderAn encoder instance to use to run the encoding.
completionA closure to execute once the document has been successfully written to the server. This closure will not be called while the client is offline, though local changes will be visible immediately.
-
Encodes an instance of
Encodableand overwrites the encoded data to the document referred by thisDocumentReference. If no document exists, it is created. If a document already exists, it is overwritten. If you pass merge:true, the providedEncodablewill be merged into any existing document.See
Firestore.Encoderfor more details about the encoding process.Declaration
Swift
func setData<T: Encodable>(from value: T, merge: Bool, encoder: Firestore.Encoder = Firestore.Encoder(), completion: ((Error?) -> Void)? = nil) throwsParameters
valueAn instance of
Encodableto be encoded to a document.mergeWhether to merge the provided
Encodableinto any existing document.encoderAn encoder instance to use to run the encoding.
completionA closure to execute once the document has been successfully written to the server. This closure will not be called while the client is offline, though local changes will be visible immediately.
-
Encodes an instance of
Encodableand writes the encoded data to the document referred by thisDocumentReferenceby 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
mergeFieldsthat does not have a corresponding field in theEncodable.See
Firestore.Encoderfor more details about the encoding process.Declaration
Swift
func setData<T: Encodable>(from value: T, mergeFields: [Any], encoder: Firestore.Encoder = Firestore.Encoder(), completion: ((Error?) -> Void)? = nil) throwsParameters
valueAn instance of
Encodableto be encoded to a document.mergeFieldsArray of
StringorFieldPathelements specifying which fields to merge. Fields can contain dots to reference nested fields within the document.encoderAn encoder instance to use to run the encoding.
completionA closure to execute once the document has been successfully written to the server. This closure will not be called while the client is offline, though local changes will be visible immediately.