aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source/API/FIRWriteBatch.mm
blob: 366c7086564f190c7e8efdc459ffb1fc9d315993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "Firestore/Source/API/FIRWriteBatch+Internal.h"

#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
#import "Firestore/Source/API/FIRFirestore+Internal.h"
#import "Firestore/Source/API/FSTUserDataConverter.h"
#import "Firestore/Source/Core/FSTFirestoreClient.h"
#import "Firestore/Source/Model/FSTMutation.h"
#import "Firestore/Source/Util/FSTUsageValidation.h"

#include "Firestore/core/src/firebase/firestore/model/precondition.h"

using firebase::firestore::model::Precondition;

NS_ASSUME_NONNULL_BEGIN

#pragma mark - FIRWriteBatch

@interface FIRWriteBatch ()

- (instancetype)initWithFirestore:(FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;

@property(nonatomic, strong, readonly) FIRFirestore *firestore;
@property(nonatomic, strong, readonly) NSMutableArray<FSTMutation *> *mutations;
@property(nonatomic, assign) BOOL committed;

@end

@implementation FIRWriteBatch (Internal)

+ (instancetype)writeBatchWithFirestore:(FIRFirestore *)firestore {
  return [[FIRWriteBatch alloc] initWithFirestore:firestore];
}

@end

@implementation FIRWriteBatch

- (instancetype)initWithFirestore:(FIRFirestore *)firestore {
  self = [super init];
  if (self) {
    _firestore = firestore;
    _mutations = [NSMutableArray array];
  }
  return self;
}

- (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
               forDocument:(FIRDocumentReference *)document {
  return [self setData:data forDocument:document merge:NO];
}

- (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
               forDocument:(FIRDocumentReference *)document
                     merge:(BOOL)merge {
  [self verifyNotCommitted];
  [self validateReference:document];
  FSTParsedSetData *parsed = merge
                                 ? [self.firestore.dataConverter parsedMergeData:data fieldMask:nil]
                                 : [self.firestore.dataConverter parsedSetData:data];
  [self.mutations
      addObjectsFromArray:[parsed mutationsWithKey:document.key precondition:Precondition::None()]];
  return self;
}

- (FIRWriteBatch *)setData:(NSDictionary<NSString *, id> *)data
               forDocument:(FIRDocumentReference *)document
               mergeFields:(NSArray<id> *)mergeFields {
  [self verifyNotCommitted];
  [self validateReference:document];
  FSTParsedSetData *parsed =
      [self.firestore.dataConverter parsedMergeData:data fieldMask:mergeFields];
  [self.mutations
      addObjectsFromArray:[parsed mutationsWithKey:document.key precondition:Precondition::None()]];
  return self;
}

- (FIRWriteBatch *)updateData:(NSDictionary<id, id> *)fields
                  forDocument:(FIRDocumentReference *)document {
  [self verifyNotCommitted];
  [self validateReference:document];
  FSTParsedUpdateData *parsed = [self.firestore.dataConverter parsedUpdateData:fields];
  [self.mutations addObjectsFromArray:[parsed mutationsWithKey:document.key
                                                  precondition:Precondition::Exists(true)]];
  return self;
}

- (FIRWriteBatch *)deleteDocument:(FIRDocumentReference *)document {
  [self verifyNotCommitted];
  [self validateReference:document];
  [self.mutations addObject:[[FSTDeleteMutation alloc] initWithKey:document.key
                                                      precondition:Precondition::None()]];
  return self;
}

- (void)commit {
  [self commitWithCompletion:nil];
}

- (void)commitWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
  [self verifyNotCommitted];
  self.committed = TRUE;
  [self.firestore.client writeMutations:self.mutations completion:completion];
}

- (void)verifyNotCommitted {
  if (self.committed) {
    FSTThrowInvalidUsage(@"FIRIllegalStateException",
                         @"A write batch can no longer be used after commit has been called.");
  }
}

- (void)validateReference:(FIRDocumentReference *)reference {
  if (reference.firestore != self.firestore) {
    FSTThrowInvalidArgument(@"Provided document reference is from a different Firestore instance.");
  }
}

@end

NS_ASSUME_NONNULL_END