CollectionReference+Combine.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Combine
  15. import FirebaseFirestore
  16. @available(swift 5.0)
  17. @available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *)
  18. public extension CollectionReference {
  19. // MARK: - Adding Documents
  20. /// Adds a new document to this collection with the specified data, assigning it a document ID
  21. /// automatically.
  22. ///
  23. /// - Parameter data: A `Dictionary` containing the data for the new document.
  24. /// - Returns: A publisher emitting a `DocumentReference` value once the document has been
  25. /// successfully written to the server. This publisher will not emit while the
  26. /// client is offline, though local changes will be visible immediately.
  27. func addDocument(data: [String: Any])
  28. -> Future<DocumentReference, Error> {
  29. var reference: DocumentReference?
  30. return Future { promise in
  31. reference = self.addDocument(data: data) { error in
  32. if let error {
  33. promise(.failure(error))
  34. } else if let reference {
  35. promise(.success(reference))
  36. }
  37. }
  38. }
  39. }
  40. /// Adds a new document to this collection with the specified data, assigning it a document ID
  41. /// automatically.
  42. ///
  43. /// - Parameters:
  44. /// - value: An instance of `Encodable` to be encoded to a document.
  45. /// - encoder: An encoder instance to use to run the encoding.
  46. /// - Returns: A publisher emitting a `DocumentReference` value once the document has been
  47. /// successfully written to the server. This publisher will not emit while the
  48. /// client is offline, though local changes will be visible immediately.
  49. func addDocument<T: Encodable>(from value: T,
  50. encoder: Firestore.Encoder = Firestore
  51. .Encoder()) -> Future<
  52. DocumentReference,
  53. Error
  54. > {
  55. var reference: DocumentReference?
  56. return Future { promise in
  57. do {
  58. try reference = self.addDocument(from: value, encoder: encoder) { error in
  59. if let error {
  60. promise(.failure(error))
  61. } else if let reference {
  62. promise(.success(reference))
  63. }
  64. }
  65. } catch {
  66. promise(.failure(error))
  67. }
  68. }
  69. }
  70. }