From e7bad94043af49b782cc39a7a5aa5636de60700a Mon Sep 17 00:00:00 2001 From: Seyed Mojtaba Hosseini Zeidabadi Date: Fri, 26 Jul 2024 14:25:55 +0330 Subject: [PATCH 1/7] feat: add the async counterpart of the `addDocument` function with the encodable --- .../CollectionReference+AsyncAwait.swift | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift b/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift index 7fb8e307e03..71223d6d1a6 100644 --- a/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift +++ b/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift @@ -42,4 +42,30 @@ public extension CollectionReference { } } } + + /// 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. + /// + /// - Parameters: + /// - value: An instance of `Encodable` to be encoded to a document. + /// - encoder: An encoder instance to use to run the encoding. + /// - Returns: A `DocumentReference` pointing to the newly created document. + @discardableResult + func addDocument(from value: T, + encoder: Firestore.Encoder = Firestore.Encoder()) async throws + -> DocumentReference { + return try await withCheckedThrowingContinuation { continuation in + var document: DocumentReference? + document = self.addDocument(from: value, encoder: encoder) { error in + if let error { + continuation.resume(throwing: error) + } else { + // Our callbacks guarantee that we either return an error or a document. + continuation.resume(returning: document!) + } + } + } + } } From b2a49665ccaad642c4cb689e7f55a3130ab10988 Mon Sep 17 00:00:00 2001 From: Seyed Mojtaba Hosseini Zeidabadi Date: Fri, 26 Jul 2024 14:28:37 +0330 Subject: [PATCH 2/7] doc: add throws part --- .../Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift b/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift index 71223d6d1a6..46e3b87fa07 100644 --- a/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift +++ b/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift @@ -51,6 +51,7 @@ public extension CollectionReference { /// - Parameters: /// - value: An instance of `Encodable` to be encoded to a document. /// - encoder: An encoder instance to use to run the encoding. + /// - Throws: `Error` if the backend rejected the write. /// - Returns: A `DocumentReference` pointing to the newly created document. @discardableResult func addDocument(from value: T, From ac759d084fab5366e5ac659dbaf8b117c197fdf9 Mon Sep 17 00:00:00 2001 From: Seyed Mojtaba Hosseini Zeidabadi Date: Fri, 26 Jul 2024 15:20:44 +0330 Subject: [PATCH 3/7] doc: enhance doc formatting --- .../Source/AsyncAwait/CollectionReference+AsyncAwait.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift b/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift index 46e3b87fa07..5d6701d0651 100644 --- a/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift +++ b/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift @@ -51,7 +51,7 @@ public extension CollectionReference { /// - Parameters: /// - value: An instance of `Encodable` to be encoded to a document. /// - encoder: An encoder instance to use to run the encoding. - /// - Throws: `Error` if the backend rejected the write. + /// - Throws: `Error` if the backend rejected the write. /// - Returns: A `DocumentReference` pointing to the newly created document. @discardableResult func addDocument(from value: T, From 4b9e3b8e6ccb39e79bb3342c23e4427c1def2204 Mon Sep 17 00:00:00 2001 From: Seyed Mojtaba Hosseini Zeidabadi Date: Fri, 26 Jul 2024 22:58:19 +0330 Subject: [PATCH 4/7] style: reformat a line to satisfy the linter line style check --- .../Source/AsyncAwait/CollectionReference+AsyncAwait.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift b/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift index 5d6701d0651..29da86d55bb 100644 --- a/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift +++ b/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift @@ -56,7 +56,7 @@ public extension CollectionReference { @discardableResult func addDocument(from value: T, encoder: Firestore.Encoder = Firestore.Encoder()) async throws - -> DocumentReference { + -> DocumentReference { return try await withCheckedThrowingContinuation { continuation in var document: DocumentReference? document = self.addDocument(from: value, encoder: encoder) { error in From 73a2461ae4701a21051028ef7b79cd8eb3fdd55a Mon Sep 17 00:00:00 2001 From: Seyed Mojtaba Hosseini Zeidabadi Date: Sat, 27 Jul 2024 07:43:47 +0330 Subject: [PATCH 5/7] style: move the function closer to the non-async implementation Asked by paulb777 [here](https://github.com/firebase/firebase-ios-sdk/pull/13407#issuecomment-2253620631) --- .../CollectionReference+AsyncAwait.swift | 27 ------------------- .../CollectionReference+WriteEncodable.swift | 27 +++++++++++++++++++ 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift b/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift index 29da86d55bb..7fb8e307e03 100644 --- a/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift +++ b/Firestore/Swift/Source/AsyncAwait/CollectionReference+AsyncAwait.swift @@ -42,31 +42,4 @@ public extension CollectionReference { } } } - - /// 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. - /// - /// - Parameters: - /// - value: An instance of `Encodable` to be encoded to a document. - /// - encoder: An encoder instance to use to run the encoding. - /// - Throws: `Error` if the backend rejected the write. - /// - Returns: A `DocumentReference` pointing to the newly created document. - @discardableResult - func addDocument(from value: T, - encoder: Firestore.Encoder = Firestore.Encoder()) async throws - -> DocumentReference { - return try await withCheckedThrowingContinuation { continuation in - var document: DocumentReference? - document = self.addDocument(from: value, encoder: encoder) { error in - if let error { - continuation.resume(throwing: error) - } else { - // Our callbacks guarantee that we either return an error or a document. - continuation.resume(returning: document!) - } - } - } - } } diff --git a/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift b/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift index c85a1fe261f..e46b987110c 100644 --- a/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift +++ b/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift @@ -44,4 +44,31 @@ public extension CollectionReference { let encoded = try encoder.encode(value) return addDocument(data: encoded, completion: completion) } + + /// 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. + /// + /// - Parameters: + /// - value: An instance of `Encodable` to be encoded to a document. + /// - encoder: An encoder instance to use to run the encoding. + /// - Throws: `Error` if the backend rejected the write. + /// - Returns: A `DocumentReference` pointing to the newly created document. + @discardableResult + func addDocument(from value: T, + encoder: Firestore.Encoder = Firestore.Encoder()) async throws + -> DocumentReference { + return try await withCheckedThrowingContinuation { continuation in + var document: DocumentReference? + document = self.addDocument(from: value, encoder: encoder) { error in + if let error { + continuation.resume(throwing: error) + } else { + // Our callbacks guarantee that we either return an error or a document. + continuation.resume(returning: document!) + } + } + } + } } From 4e13fe5db5c84b6b23dc61c06f286378bacb039c Mon Sep 17 00:00:00 2001 From: Seyed Mojtaba Hosseini Zeidabadi Date: Sat, 27 Jul 2024 15:51:03 +0330 Subject: [PATCH 6/7] feat: add availability to support concurrency --- .../Source/Codable/CollectionReference+WriteEncodable.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift b/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift index e46b987110c..d1e9d80aaae 100644 --- a/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift +++ b/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift @@ -55,6 +55,7 @@ public extension CollectionReference { /// - encoder: An encoder instance to use to run the encoding. /// - Throws: `Error` if the backend rejected the write. /// - Returns: A `DocumentReference` pointing to the newly created document. + @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *) @discardableResult func addDocument(from value: T, encoder: Firestore.Encoder = Firestore.Encoder()) async throws From 77e8c609a14f0d049afe5cc6d3206a29e68b3a05 Mon Sep 17 00:00:00 2001 From: Seyed Mojtaba Hosseini Zeidabadi Date: Sat, 27 Jul 2024 15:52:26 +0330 Subject: [PATCH 7/7] feat: catch the throwing error and resume it to the continuation --- .../CollectionReference+WriteEncodable.swift | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift b/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift index d1e9d80aaae..e4d52eab7f7 100644 --- a/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift +++ b/Firestore/Swift/Source/Codable/CollectionReference+WriteEncodable.swift @@ -62,13 +62,17 @@ public extension CollectionReference { -> DocumentReference { return try await withCheckedThrowingContinuation { continuation in var document: DocumentReference? - document = self.addDocument(from: value, encoder: encoder) { error in - if let error { - continuation.resume(throwing: error) - } else { - // Our callbacks guarantee that we either return an error or a document. - continuation.resume(returning: document!) + do { + document = try self.addDocument(from: value, encoder: encoder) { error in + if let error { + continuation.resume(throwing: error) + } else { + // Our callbacks guarantee that we either return an error or a document. + continuation.resume(returning: document!) + } } + } catch { + continuation.resume(throwing: error) } } }