FIRWriteBatch.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 `Firestore.batch()`. It provides methods for
  23. * adding writes to the write batch. None of the writes will be committed (or visible locally)
  24. * until `WriteBatch.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 A `Dictionary` 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 `WriteBatch` 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:true`, the provided data will
  49. * be merged into any existing document.
  50. *
  51. * @param data A `Dictionary` 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. If enabled,
  54. * all omitted fields remain untouched. If your input sets any field to an empty dictionary, any
  55. * nested field is overwritten.
  56. * @return This `WriteBatch` instance. Used for chaining method calls.
  57. */
  58. // clang-format off
  59. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  60. forDocument:(FIRDocumentReference *)document
  61. merge:(BOOL)merge
  62. NS_SWIFT_NAME(setData(_:forDocument:merge:));
  63. // clang-format on
  64. /**
  65. * Writes to the document referred to by `document` and only replace the fields
  66. * specified under `mergeFields`. Any field that is not specified in `mergeFields`
  67. * is ignored and remains untouched. If the document doesn't yet exist,
  68. * this method creates it and then sets the data.
  69. *
  70. * It is an error to include a field in `mergeFields` that does not have a corresponding
  71. * value in the `data` dictionary.
  72. *
  73. * @param data A `Dictionary` that contains the fields and data to write to the document.
  74. * @param document A reference to the document whose data should be overwritten.
  75. * @param mergeFields An `Array` that contains a list of `String` or `FieldPath` elements
  76. * specifying which fields to merge. Fields can contain dots to reference nested fields within
  77. * the document. If your input sets any field to an empty dictionary, any nested field is
  78. * overwritten.
  79. * @return This `WriteBatch` instance. Used for chaining method calls.
  80. */
  81. // clang-format off
  82. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  83. forDocument:(FIRDocumentReference *)document
  84. mergeFields:(NSArray<id> *)mergeFields
  85. NS_SWIFT_NAME(setData(_:forDocument:mergeFields:));
  86. // clang-format on
  87. /**
  88. * Updates fields in the document referred to by `document`.
  89. * If document does not exist, the write batch will fail.
  90. *
  91. * @param fields A `Dictionary` containing the fields (expressed as an `String` or
  92. * `FieldPath`) and values with which to update the document.
  93. * @param document A reference to the document whose data should be updated.
  94. * @return This `WriteBatch` instance. Used for chaining method calls.
  95. */
  96. // clang-format off
  97. - (FIRWriteBatch *)updateData:(NSDictionary<id, id> *)fields
  98. forDocument:(FIRDocumentReference *)document
  99. NS_SWIFT_NAME(updateData(_:forDocument:));
  100. // clang-format on
  101. /**
  102. * Deletes the document referred to by `document`.
  103. *
  104. * @param document A reference to the document that should be deleted.
  105. * @return This `WriteBatch` instance. Used for chaining method calls.
  106. */
  107. - (FIRWriteBatch *)deleteDocument:(FIRDocumentReference *)document
  108. NS_SWIFT_NAME(deleteDocument(_:));
  109. /**
  110. * Commits all of the writes in this write batch as a single atomic unit.
  111. */
  112. - (void)commit;
  113. /**
  114. * Commits all of the writes in this write batch as a single atomic unit.
  115. *
  116. * @param completion A block to be called once all of the writes in the batch have been
  117. * successfully written to the backend as an atomic unit. This block will only execute
  118. * when the client is online and the commit has completed against the server. The
  119. * completion handler will not be called when the device is offline, though local
  120. * changes will be visible immediately.
  121. */
  122. - (void)commitWithCompletion:(nullable void (^)(NSError *_Nullable error))completion;
  123. @end
  124. NS_ASSUME_NONNULL_END