Skip to content

Commit e7bad94

Browse files
committed
feat: add the async counterpart of the addDocument function with the encodable
1 parent 1cdba8e commit e7bad94

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,30 @@ public extension CollectionReference {
4242
}
4343
}
4444
}
45+
46+
/// Encodes an instance of `Encodable` and adds a new document to this collection
47+
/// with the encoded data, assigning it a document ID automatically.
48+
///
49+
/// See `Firestore.Encoder` for more details about the encoding process.
50+
///
51+
/// - Parameters:
52+
/// - value: An instance of `Encodable` to be encoded to a document.
53+
/// - encoder: An encoder instance to use to run the encoding.
54+
/// - Returns: A `DocumentReference` pointing to the newly created document.
55+
@discardableResult
56+
func addDocument<T: Encodable>(from value: T,
57+
encoder: Firestore.Encoder = Firestore.Encoder()) async throws
58+
-> DocumentReference {
59+
return try await withCheckedThrowingContinuation { continuation in
60+
var document: DocumentReference?
61+
document = self.addDocument(from: value, encoder: encoder) { error in
62+
if let error {
63+
continuation.resume(throwing: error)
64+
} else {
65+
// Our callbacks guarantee that we either return an error or a document.
66+
continuation.resume(returning: document!)
67+
}
68+
}
69+
}
70+
}
4571
}

0 commit comments

Comments
 (0)