FIRWriteBatch.mm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "Firestore/Source/API/FIRWriteBatch+Internal.h"
  17. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  18. #import "Firestore/Source/API/FSTUserDataReader.h"
  19. #include "Firestore/core/src/api/write_batch.h"
  20. #include "Firestore/core/src/core/user_data.h"
  21. #include "Firestore/core/src/util/delayed_constructor.h"
  22. #include "Firestore/core/src/util/error_apple.h"
  23. using firebase::firestore::core::ParsedSetData;
  24. using firebase::firestore::core::ParsedUpdateData;
  25. using firebase::firestore::util::DelayedConstructor;
  26. using firebase::firestore::util::MakeCallback;
  27. NS_ASSUME_NONNULL_BEGIN
  28. #pragma mark - FIRWriteBatch
  29. @interface FIRWriteBatch ()
  30. - (instancetype)initWithDataReader:(FSTUserDataReader *)dataReader
  31. writeBatch:(api::WriteBatch &&)writeBatch NS_DESIGNATED_INITIALIZER;
  32. @property(nonatomic, strong, readonly) FSTUserDataReader *dataReader;
  33. @end
  34. @implementation FIRWriteBatch (Internal)
  35. + (instancetype)writeBatchWithDataReader:(FSTUserDataReader *)dataReader
  36. writeBatch:(api::WriteBatch &&)writeBatch {
  37. return [[FIRWriteBatch alloc] initWithDataReader:dataReader writeBatch:std::move(writeBatch)];
  38. }
  39. @end
  40. @implementation FIRWriteBatch {
  41. DelayedConstructor<api::WriteBatch> _writeBatch;
  42. }
  43. - (instancetype)initWithDataReader:(FSTUserDataReader *)dataReader
  44. writeBatch:(api::WriteBatch &&)writeBatch {
  45. self = [super init];
  46. if (self) {
  47. _dataReader = dataReader;
  48. _writeBatch.Init(std::move(writeBatch));
  49. }
  50. return self;
  51. }
  52. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  53. forDocument:(FIRDocumentReference *)document {
  54. return [self setData:data forDocument:document merge:NO];
  55. }
  56. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  57. forDocument:(FIRDocumentReference *)document
  58. merge:(BOOL)merge {
  59. ParsedSetData parsed = merge ? [self.dataReader parsedMergeData:data fieldMask:nil]
  60. : [self.dataReader parsedSetData:data];
  61. _writeBatch->SetData(document.internalReference, std::move(parsed));
  62. return self;
  63. }
  64. - (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
  65. forDocument:(FIRDocumentReference *)document
  66. mergeFields:(NSArray<id> *)mergeFields {
  67. ParsedSetData parsed = [self.dataReader parsedMergeData:data fieldMask:mergeFields];
  68. _writeBatch->SetData(document.internalReference, std::move(parsed));
  69. return self;
  70. }
  71. - (FIRWriteBatch *)updateData:(NSDictionary<id, id> *)fields
  72. forDocument:(FIRDocumentReference *)document {
  73. ParsedUpdateData parsed = [self.dataReader parsedUpdateData:fields];
  74. _writeBatch->UpdateData(document.internalReference, std::move(parsed));
  75. return self;
  76. }
  77. - (FIRWriteBatch *)deleteDocument:(FIRDocumentReference *)document {
  78. _writeBatch->DeleteData(document.internalReference);
  79. return self;
  80. }
  81. - (void)commit {
  82. [self commitWithCompletion:nil];
  83. }
  84. - (void)commitWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
  85. _writeBatch->Commit(MakeCallback(completion));
  86. }
  87. @end
  88. NS_ASSUME_NONNULL_END