FIRWriteBatch.mm 3.6 KB

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