FirebaseFirestoreSwift Framework Reference

CollectionReference

public extension CollectionReference
  • Adds a new document to this collection with the specified data, assigning it a document ID automatically.

    Throws

    Error if the backend rejected the write.

    Declaration

    Swift

    func addDocument(data: [String : Any]) async throws -> DocumentReference

    Parameters

    data

    A Dictionary containing the data for the new document.

    Return Value

    A DocumentReference pointing to the newly created document.

  • Encodes an instance of Encodable and adds a new document to this collection with the encoded data, assigning it a document ID automatically.

    See Firestore.Encoder for more details about the encoding process.

    Declaration

    Swift

    func addDocument<T: Encodable>(from value: T,
                                   encoder: Firestore.Encoder = Firestore.Encoder(),
                                   completion: ((Error?) -> Void)? = nil) throws
      -> DocumentReference

    Parameters

    value

    An instance of Encodable to be encoded to a document.

    encoder

    An encoder instance to use to run the encoding.

    completion

    A block to execute once the document has been successfully written to the server. This block will not be called while the client is offline, though local changes will be visible immediately.

    Return Value

    A DocumentReference pointing to the newly created document.