Firestore+AsyncAwait.swift 5.3 KB

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