FIRWriteBatch.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2017 Google
  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/Foundation.h>
  17. NS_ASSUME_NONNULL_BEGIN
  18. @class FIRDocumentReference;
  19. /**
  20. * A write batch is used to perform multiple writes as a single atomic unit.
  21. *
  22. * A WriteBatch object can be acquired by calling [FIRFirestore batch]. It provides methods for
  23. * adding writes to the write batch. None of the writes will be committed (or visible locally)
  24. * until [FIRWriteBatch commit] is called.
  25. *
  26. * Unlike transactions, write batches are persisted offline and therefore are preferable when you
  27. * don't need to condition your writes on read data.
  28. */
  29. NS_SWIFT_NAME(WriteBatch)
  30. @interface FIRWriteBatch : NSObject
  31. /** :nodoc: */
  32. - (id)init __attribute__((unavailable("FIRWriteBatch cannot be created directly.")));
  33. /**
  34. * Writes to the document referred to by `document`. If the document doesn't yet exist,
  35. * this method creates it and then sets the data. If the document exists, this method overwrites
  36. * the document data with the new values.
  37. *
  38. * @param data An `NSDictionary` that contains the fields and data to write to the document.
  39. * @param document A reference to the document whose data should be overwritten.
  40. * @return This `FIRWriteBatch` instance. Used for chaining method calls.
  41. */
  42. // clang-format off
  43. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  44. forDocument:(FIRDocumentReference *)document NS_SWIFT_NAME(setData(_:forDocument:));
  45. // clang-format on
  46. /**
  47. * Writes to the document referred to by `document`. If the document doesn't yet exist,
  48. * this method creates it and then sets the data. If you pass `merge:YES`, the provided data will be
  49. * merged into any existing document.
  50. *
  51. * @param data An `NSDictionary` that contains the fields and data to write to the document.
  52. * @param document A reference to the document whose data should be overwritten.
  53. * @param merge Whether to merge the provided data into any existing document.
  54. * @return This `FIRWriteBatch` instance. Used for chaining method calls.
  55. */
  56. // clang-format off
  57. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  58. forDocument:(FIRDocumentReference *)document
  59. merge:(BOOL)merge
  60. NS_SWIFT_NAME(setData(_:forDocument:merge:));
  61. // clang-format on
  62. /**
  63. * Writes to the document referred to by `document` and only replace the fields
  64. * specified under `mergeFields`. Any field that is not specified in `mergeFields`
  65. * is ignored and remains untouched. If the document doesn't yet exist,
  66. * this method creates it and then sets the data.
  67. *
  68. * It is an error to include a field in `mergeFields` that does not have a corresponding
  69. * value in the `data` dictionary.
  70. *
  71. * @param data An `NSDictionary` that contains the fields and data to write to the document.
  72. * @param document A reference to the document whose data should be overwritten.
  73. * @param mergeFields An `NSArray` that contains a list of `NSString` or `FIRFieldPath` elements
  74. * specifying which fields to merge. Fields can contain dots to reference nested fields within
  75. * the document.
  76. * @return This `FIRWriteBatch` instance. Used for chaining method calls.
  77. */
  78. // clang-format off
  79. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  80. forDocument:(FIRDocumentReference *)document
  81. mergeFields:(NSArray<id> *)mergeFields
  82. NS_SWIFT_NAME(setData(_:forDocument:mergeFields:));
  83. // clang-format on
  84. /**
  85. * Updates fields in the document referred to by `document`.
  86. * If document does not exist, the write batch will fail.
  87. *
  88. * @param fields An `NSDictionary` containing the fields (expressed as an `NSString` or
  89. * `FIRFieldPath`) and values with which to update the document.
  90. * @param document A reference to the document whose data should be updated.
  91. * @return This `FIRWriteBatch` instance. Used for chaining method calls.
  92. */
  93. // clang-format off
  94. - (FIRWriteBatch *)updateData:(NSDictionary<id, id> *)fields
  95. forDocument:(FIRDocumentReference *)document
  96. NS_SWIFT_NAME(updateData(_:forDocument:));
  97. // clang-format on
  98. /**
  99. * Deletes the document referred to by `document`.
  100. *
  101. * @param document A reference to the document that should be deleted.
  102. * @return This `FIRWriteBatch` instance. Used for chaining method calls.
  103. */
  104. - (FIRWriteBatch *)deleteDocument:(FIRDocumentReference *)document
  105. NS_SWIFT_NAME(deleteDocument(_:));
  106. /**
  107. * Commits all of the writes in this write batch as a single atomic unit.
  108. */
  109. - (void)commit;
  110. /**
  111. * Commits all of the writes in this write batch as a single atomic unit.
  112. *
  113. * @param completion A block to be called once all of the writes in the batch have been
  114. * successfully written to the backend as an atomic unit. This block will only execute
  115. * when the client is online and the commit has completed against the server. The
  116. * completion handler will not be called when the device is offline, though local
  117. * changes will be visible immediately.
  118. */
  119. - (void)commitWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;
  120. @end
  121. NS_ASSUME_NONNULL_END