Firestore+AsyncAwait.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #if SWIFT_PACKAGE
  17. import FirebaseFirestoreCpp
  18. @_exported import FirebaseFirestoreInternalWrapper
  19. #else
  20. @_exported import FirebaseFirestoreInternal
  21. #endif // SWIFT_PACKAGE
  22. import Foundation
  23. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  24. public extension Firestore {
  25. /// Loads a Firestore bundle into the local cache.
  26. /// - Parameter bundleData: Data from the bundle to be loaded.
  27. /// - Throws: `Error` if the bundle data cannot be parsed.
  28. /// - Returns: The final `LoadBundleTaskProgress` that contains the total number of documents
  29. /// loaded.
  30. func loadBundle(_ bundleData: Data) async throws -> LoadBundleTaskProgress {
  31. return try await withCheckedThrowingContinuation { continuation in
  32. self.loadBundle(bundleData) { progress, error in
  33. if let error {
  34. continuation.resume(throwing: error)
  35. } else {
  36. // Our callbacks guarantee that we either return an error or a progress event.
  37. continuation.resume(returning: progress!)
  38. }
  39. }
  40. }
  41. }
  42. /// Loads a Firestore bundle into the local cache.
  43. /// - Parameter bundleStream: An input stream from which the bundle can be read.
  44. /// - Throws: `Error` if the bundle stream cannot be parsed.
  45. /// - Returns: The final `LoadBundleTaskProgress` that contains the total number of documents
  46. /// loaded.
  47. func loadBundle(_ bundleStream: InputStream) async throws -> LoadBundleTaskProgress {
  48. return try await withCheckedThrowingContinuation { continuation in
  49. self.loadBundle(bundleStream) { progress, error in
  50. if let error {
  51. continuation.resume(throwing: error)
  52. } else {
  53. // Our callbacks guarantee that we either return an error or a progress event.
  54. continuation.resume(returning: progress!)
  55. }
  56. }
  57. }
  58. }
  59. /// Executes the given updateBlock and then attempts to commit the changes applied within an
  60. /// atomic
  61. /// transaction.
  62. ///
  63. /// The maximum number of writes allowed in a single transaction is 500, but note that each
  64. /// usage of
  65. /// `FieldValue.serverTimestamp()`, `FieldValue.arrayUnion()`, `FieldValue.arrayRemove()`, or
  66. /// `FieldValue.increment()` inside a transaction counts as an additional write.
  67. ///
  68. /// In the `updateBlock`, a set of reads and writes can be performed atomically using the
  69. /// `Transaction` object passed to the block. After the `updateBlock` is run, Firestore will
  70. /// attempt
  71. /// to apply the changes to the server. If any of the data read has been modified outside of
  72. /// this
  73. /// transaction since being read, then the transaction will be retried by executing the
  74. /// `updateBlock`
  75. /// again. If the transaction still fails after 5 retries, then the transaction will fail.
  76. ///
  77. /// Since the `updateBlock` may be executed multiple times, it should avoiding doing anything
  78. /// that
  79. /// would cause side effects.
  80. ///
  81. /// Any value maybe be returned from the `updateBlock`. If the transaction is successfully
  82. /// committed,
  83. /// then the completion block will be passed that value. The `updateBlock` also has an `NSError`
  84. /// out
  85. /// parameter. If this is set, then the transaction will not attempt to commit, and the given
  86. /// error
  87. /// will be returned.
  88. ///
  89. /// The `Transaction` object passed to the `updateBlock` contains methods for accessing
  90. /// documents
  91. /// and collections. Unlike other firestore access, data accessed with the transaction will not
  92. /// reflect local changes that have not been committed. For this reason, it is required that all
  93. /// reads are performed before any writes. Transactions must be performed while online.
  94. /// Otherwise,
  95. /// reads will fail, the final commit will fail, and this function will return an error.
  96. ///
  97. /// - Parameter updateBlock The block to execute within the transaction context.
  98. /// - Throws Throws an error if the transaction could not be committed, or if an error was
  99. /// explicitly specified in the `updateBlock` parameter.
  100. /// - Returns Returns the value returned in the `updateBlock` parameter if no errors occurred.
  101. func runTransaction(_ updateBlock: @escaping (Transaction, NSErrorPointer)
  102. -> Any?) async throws -> Any? {
  103. // This needs to be wrapped in order to express a nullable return value upon success.
  104. // See https://github.com/firebase/firebase-ios-sdk/issues/9426 for more details.
  105. return try await withCheckedThrowingContinuation { continuation in
  106. self.runTransaction(updateBlock) { anyValue, error in
  107. if let error {
  108. continuation.resume(throwing: error)
  109. } else {
  110. continuation.resume(returning: anyValue)
  111. }
  112. }
  113. }
  114. }
  115. func pipeline() -> PipelineSource {
  116. return PipelineSource(firebase.firestore.api.FirestorePipeline.pipeline(cppFirestorePtr))
  117. }
  118. }