CollectionReference+Combine.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #if canImport(Combine) && swift(>=5.0)
  15. import Combine
  16. import FirebaseFirestore
  17. #if canImport(FirebaseFirestoreSwift)
  18. import FirebaseFirestoreSwift
  19. #endif
  20. @available(swift 5.0)
  21. @available(iOS 13.0, macOS 10.15, macCatalyst 13.0, tvOS 13.0, watchOS 6.0, *)
  22. public extension CollectionReference {
  23. // MARK: - Adding Documents
  24. /// Adds a new document to this collection with the specified data, assigning it a document ID
  25. /// automatically.
  26. ///
  27. /// - Parameter data: A `Dictionary` containing the data for the new document.
  28. /// - Returns: A publisher emitting a `DocumentReference` value once the document has been successfully
  29. /// written to the server. This publisher will not emit while the client is offline, though
  30. /// local changes will be visible immediately.
  31. func addDocument(data: [String: Any])
  32. -> Future<DocumentReference, Error> {
  33. var reference: DocumentReference?
  34. return Future { promise in
  35. reference = self.addDocument(data: data) { error in
  36. if let error = error {
  37. promise(.failure(error))
  38. } else if let reference = reference {
  39. promise(.success(reference))
  40. }
  41. }
  42. }
  43. }
  44. #if canImport(FirebaseFirestoreSwift)
  45. /// Adds a new document to this collection with the specified data, assigning it a document ID
  46. /// automatically.
  47. ///
  48. /// - Parameters:
  49. /// - value: An instance of `Encodable` to be encoded to a document.
  50. /// - encoder: An encoder instance to use to run the encoding.
  51. /// - Returns: A publisher emitting a `DocumentReference` value once the document has been successfully
  52. /// written to the server. This publisher will not emit while the client is offline, though
  53. /// local changes will be visible immediately.
  54. func addDocument<T: Encodable>(from value: T,
  55. encoder: Firestore.Encoder = Firestore
  56. .Encoder()) -> Future<
  57. DocumentReference,
  58. Error
  59. > {
  60. var reference: DocumentReference?
  61. return Future { promise in
  62. do {
  63. try reference = self.addDocument(from: value, encoder: encoder) { error in
  64. if let error = error {
  65. promise(.failure(error))
  66. } else if let reference = reference {
  67. promise(.success(reference))
  68. }
  69. }
  70. } catch {
  71. promise(.failure(error))
  72. }
  73. }
  74. }
  75. #endif
  76. }
  77. #endif