DocumentReference+ReadDecodable.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 2021 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. #if SWIFT_PACKAGE
  18. @_exported import FirebaseFirestoreInternalWrapper
  19. #else
  20. @_exported import FirebaseFirestoreInternal
  21. #endif // SWIFT_PACKAGE
  22. public extension DocumentReference {
  23. /// Fetches and decodes the document referenced by this `DocumentReference`.
  24. ///
  25. /// This allows users to retrieve a Firestore document and have it decoded to
  26. /// an instance of caller-specified type as follows:
  27. /// ```swift
  28. /// ref.getDocument(as: Book.self) { result in
  29. /// do {
  30. /// let book = try result.get()
  31. /// } catch {
  32. /// // Handle error
  33. /// }
  34. /// }
  35. /// ```
  36. ///
  37. /// This method attempts to provide up-to-date data when possible by waiting
  38. /// for data from the server, but it may return cached data or fail if you are
  39. /// offline and the server cannot be reached. If `T` denotes an optional
  40. /// type, the method returns a successful status with a value of `nil` for
  41. /// non-existing documents.
  42. ///
  43. /// - Parameters:
  44. /// - as: A `Decodable` type to convert the document fields to.
  45. /// - serverTimestampBehavior: Configures how server timestamps that have
  46. /// not yet been set to their final value are returned from the snapshot.
  47. /// - decoder: The decoder to use to convert the document. Defaults to use
  48. /// the default decoder.
  49. /// - completion: The closure to call when the document snapshot has been
  50. /// fetched and decoded.
  51. func getDocument<T: Decodable>(as type: T.Type,
  52. with serverTimestampBehavior: ServerTimestampBehavior =
  53. .none,
  54. decoder: Firestore.Decoder = .init(),
  55. completion: @escaping (Result<T, Error>) -> Void) {
  56. getDocument { snapshot, error in
  57. guard let snapshot = snapshot else {
  58. /**
  59. * Force unwrapping here is fine since this logic corresponds to the auto-synthesized
  60. * async/await wrappers for Objective-C functions with callbacks taking an object and an error
  61. * parameter. The API should (and does) guarantee that either object or error is set, but never both.
  62. * For more details see:
  63. * https://github.com/firebase/firebase-ios-sdk/pull/9101#discussion_r809117034
  64. */
  65. completion(.failure(error!))
  66. return
  67. }
  68. let result = Result {
  69. try snapshot.data(as: T.self,
  70. with: serverTimestampBehavior,
  71. decoder: decoder)
  72. }
  73. completion(result)
  74. }
  75. }
  76. /// Fetches and decodes the document referenced by this `DocumentReference`.
  77. ///
  78. /// This allows users to retrieve a Firestore document and have it decoded
  79. /// to an instance of caller-specified type as follows:
  80. /// ```swift
  81. /// do {
  82. /// let book = try await ref.getDocument(as: Book.self)
  83. /// } catch {
  84. /// // Handle error
  85. /// }
  86. /// ```
  87. ///
  88. /// This method attempts to provide up-to-date data when possible by waiting
  89. /// for data from the server, but it may return cached data or fail if you
  90. /// are offline and the server cannot be reached. If `T` denotes
  91. /// an optional type, the method returns a successful status with a value
  92. /// of `nil` for non-existing documents.
  93. ///
  94. /// - Parameters:
  95. /// - as: A `Decodable` type to convert the document fields to.
  96. /// - serverTimestampBehavior: Configures how server timestamps that have
  97. /// not yet been set to their final value are returned from the
  98. /// snapshot.
  99. /// - decoder: The decoder to use to convert the document. Defaults to use
  100. /// the default decoder.
  101. /// - Returns: This instance of the supplied `Decodable` type `T`.
  102. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  103. func getDocument<T: Decodable>(as type: T.Type,
  104. with serverTimestampBehavior: ServerTimestampBehavior =
  105. .none,
  106. decoder: Firestore.Decoder = .init()) async throws -> T {
  107. let snapshot = try await getDocument()
  108. return try snapshot.data(as: T.self,
  109. with: serverTimestampBehavior,
  110. decoder: decoder)
  111. }
  112. }