-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
FIRDocumentReference.mm
311 lines (254 loc) · 11.7 KB
/
FIRDocumentReference.mm
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* 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 "FIRDocumentReference.h"
#import <GRPCClient/GRPCCall.h>
#import "FIRFirestoreErrors.h"
#import "FIRSnapshotMetadata.h"
#import "Firestore/Source/API/FIRCollectionReference+Internal.h"
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
#import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
#import "Firestore/Source/API/FIRFirestore+Internal.h"
#import "Firestore/Source/API/FIRListenerRegistration+Internal.h"
#import "Firestore/Source/API/FIRSetOptions+Internal.h"
#import "Firestore/Source/API/FSTUserDataConverter.h"
#import "Firestore/Source/Core/FSTEventManager.h"
#import "Firestore/Source/Core/FSTFirestoreClient.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Model/FSTDocumentKey.h"
#import "Firestore/Source/Model/FSTDocumentSet.h"
#import "Firestore/Source/Model/FSTFieldValue.h"
#import "Firestore/Source/Model/FSTMutation.h"
#import "Firestore/Source/Model/FSTPath.h"
#import "Firestore/Source/Util/FSTAssert.h"
#import "Firestore/Source/Util/FSTAsyncQueryListener.h"
#import "Firestore/Source/Util/FSTUsageValidation.h"
NS_ASSUME_NONNULL_BEGIN
#pragma mark - FIRDocumentListenOptions
@interface FIRDocumentListenOptions ()
- (instancetype)initWithIncludeMetadataChanges:(BOOL)includeMetadataChanges
NS_DESIGNATED_INITIALIZER;
@property(nonatomic, assign, readonly) BOOL includeMetadataChanges;
@end
@implementation FIRDocumentListenOptions
+ (instancetype)options {
return [[FIRDocumentListenOptions alloc] init];
}
- (instancetype)initWithIncludeMetadataChanges:(BOOL)includeMetadataChanges {
if (self = [super init]) {
_includeMetadataChanges = includeMetadataChanges;
}
return self;
}
- (instancetype)init {
return [self initWithIncludeMetadataChanges:NO];
}
- (instancetype)includeMetadataChanges:(BOOL)includeMetadataChanges {
return [[FIRDocumentListenOptions alloc] initWithIncludeMetadataChanges:includeMetadataChanges];
}
@end
#pragma mark - FIRDocumentReference
@interface FIRDocumentReference ()
- (instancetype)initWithKey:(FSTDocumentKey *)key
firestore:(FIRFirestore *)firestore NS_DESIGNATED_INITIALIZER;
@property(nonatomic, strong, readonly) FSTDocumentKey *key;
@end
@implementation FIRDocumentReference (Internal)
+ (instancetype)referenceWithPath:(FSTResourcePath *)path firestore:(FIRFirestore *)firestore {
if (path.length % 2 != 0) {
FSTThrowInvalidArgument(
@"Invalid document reference. Document references must have an even "
"number of segments, but %@ has %d",
path.canonicalString, path.length);
}
return
[FIRDocumentReference referenceWithKey:[FSTDocumentKey keyWithPath:path] firestore:firestore];
}
+ (instancetype)referenceWithKey:(FSTDocumentKey *)key firestore:(FIRFirestore *)firestore {
return [[FIRDocumentReference alloc] initWithKey:key firestore:firestore];
}
@end
@implementation FIRDocumentReference
- (instancetype)initWithKey:(FSTDocumentKey *)key firestore:(FIRFirestore *)firestore {
if (self = [super init]) {
_key = key;
_firestore = firestore;
}
return self;
}
#pragma mark - NSObject Methods
- (BOOL)isEqual:(nullable id)other {
if (other == self) return YES;
if (![[other class] isEqual:[self class]]) return NO;
return [self isEqualToReference:other];
}
- (BOOL)isEqualToReference:(nullable FIRDocumentReference *)reference {
if (self == reference) return YES;
if (reference == nil) return NO;
return [self.firestore isEqual:reference.firestore] && [self.key isEqualToKey:reference.key];
}
- (NSUInteger)hash {
NSUInteger hash = [self.firestore hash];
hash = hash * 31u + [self.key hash];
return hash;
}
#pragma mark - Public Methods
- (NSString *)documentID {
return [self.key.path lastSegment];
}
- (FIRCollectionReference *)parent {
FSTResourcePath *parentPath = [self.key.path pathByRemovingLastSegment];
return [FIRCollectionReference referenceWithPath:parentPath firestore:self.firestore];
}
- (NSString *)path {
return [self.key.path canonicalString];
}
- (FIRCollectionReference *)collectionWithPath:(NSString *)collectionPath {
if (!collectionPath) {
FSTThrowInvalidArgument(@"Collection path cannot be nil.");
}
FSTResourcePath *subPath = [FSTResourcePath pathWithString:collectionPath];
FSTResourcePath *path = [self.key.path pathByAppendingPath:subPath];
return [FIRCollectionReference referenceWithPath:path firestore:self.firestore];
}
- (void)setData:(NSDictionary<NSString *, id> *)documentData {
return [self setData:documentData options:[FIRSetOptions overwrite] completion:nil];
}
- (void)setData:(NSDictionary<NSString *, id> *)documentData options:(FIRSetOptions *)options {
return [self setData:documentData options:options completion:nil];
}
- (void)setData:(NSDictionary<NSString *, id> *)documentData
completion:(nullable void (^)(NSError *_Nullable error))completion {
return [self setData:documentData options:[FIRSetOptions overwrite] completion:completion];
}
- (void)setData:(NSDictionary<NSString *, id> *)documentData
options:(FIRSetOptions *)options
completion:(nullable void (^)(NSError *_Nullable error))completion {
FSTParsedSetData *parsed = options.isMerge
? [self.firestore.dataConverter parsedMergeData:documentData]
: [self.firestore.dataConverter parsedSetData:documentData];
return [self.firestore.client
writeMutations:[parsed mutationsWithKey:self.key precondition:[FSTPrecondition none]]
completion:completion];
}
- (void)updateData:(NSDictionary<id, id> *)fields {
return [self updateData:fields completion:nil];
}
- (void)updateData:(NSDictionary<id, id> *)fields
completion:(nullable void (^)(NSError *_Nullable error))completion {
FSTParsedUpdateData *parsed = [self.firestore.dataConverter parsedUpdateData:fields];
return [self.firestore.client
writeMutations:[parsed mutationsWithKey:self.key
precondition:[FSTPrecondition preconditionWithExists:YES]]
completion:completion];
}
- (void)deleteDocument {
return [self deleteDocumentWithCompletion:nil];
}
- (void)deleteDocumentWithCompletion:(nullable void (^)(NSError *_Nullable error))completion {
FSTDeleteMutation *mutation =
[[FSTDeleteMutation alloc] initWithKey:self.key precondition:[FSTPrecondition none]];
return [self.firestore.client writeMutations:@[ mutation ] completion:completion];
}
- (void)getDocumentWithCompletion:(void (^)(FIRDocumentSnapshot *_Nullable document,
NSError *_Nullable error))completion {
FSTListenOptions *listenOptions =
[[FSTListenOptions alloc] initWithIncludeQueryMetadataChanges:YES
includeDocumentMetadataChanges:YES
waitForSyncWhenOnline:YES];
dispatch_semaphore_t registered = dispatch_semaphore_create(0);
__block id<FIRListenerRegistration> listenerRegistration;
FIRDocumentSnapshotBlock listener = ^(FIRDocumentSnapshot *snapshot, NSError *error) {
if (error) {
completion(nil, error);
return;
}
// Remove query first before passing event to user to avoid user actions affecting the
// now stale query.
dispatch_semaphore_wait(registered, DISPATCH_TIME_FOREVER);
[listenerRegistration remove];
if (!snapshot.exists && snapshot.metadata.fromCache) {
// TODO(dimond): Reconsider how to raise missing documents when offline.
// If we're online and the document doesn't exist then we call the completion with
// a document with document.exists set to false. If we're offline however, we call the
// completion handler with an error. Two options:
// 1) Cache the negative response from the server so we can deliver that even when you're
// offline.
// 2) Actually call the completion handler with an error if the document doesn't exist when
// you are offline.
// TODO(dimond): Use proper error domain
completion(nil,
[NSError errorWithDomain:FIRFirestoreErrorDomain
code:FIRFirestoreErrorCodeUnavailable
userInfo:@{
NSLocalizedDescriptionKey :
@"Failed to get document because the client is offline.",
}]);
} else {
completion(snapshot, nil);
}
};
listenerRegistration =
[self addSnapshotListenerInternalWithOptions:listenOptions listener:listener];
dispatch_semaphore_signal(registered);
}
- (id<FIRListenerRegistration>)addSnapshotListener:(FIRDocumentSnapshotBlock)listener {
return [self addSnapshotListenerWithOptions:nil listener:listener];
}
- (id<FIRListenerRegistration>)addSnapshotListenerWithOptions:
(nullable FIRDocumentListenOptions *)options
listener:(FIRDocumentSnapshotBlock)listener {
return [self addSnapshotListenerInternalWithOptions:[self internalOptions:options]
listener:listener];
}
- (id<FIRListenerRegistration>)
addSnapshotListenerInternalWithOptions:(FSTListenOptions *)internalOptions
listener:(FIRDocumentSnapshotBlock)listener {
FIRFirestore *firestore = self.firestore;
FSTQuery *query = [FSTQuery queryWithPath:self.key.path];
FSTDocumentKey *key = self.key;
FSTViewSnapshotHandler snapshotHandler = ^(FSTViewSnapshot *snapshot, NSError *error) {
if (error) {
listener(nil, error);
return;
}
FSTAssert(snapshot.documents.count <= 1, @"Too many document returned on a document query");
FSTDocument *document = [snapshot.documents documentForKey:key];
FIRDocumentSnapshot *result = [FIRDocumentSnapshot snapshotWithFirestore:firestore
documentKey:key
document:document
fromCache:snapshot.fromCache];
listener(result, nil);
};
FSTAsyncQueryListener *asyncListener =
[[FSTAsyncQueryListener alloc] initWithDispatchQueue:self.firestore.client.userDispatchQueue
snapshotHandler:snapshotHandler];
FSTQueryListener *internalListener =
[firestore.client listenToQuery:query
options:internalOptions
viewSnapshotHandler:[asyncListener asyncSnapshotHandler]];
return [[FSTListenerRegistration alloc] initWithClient:self.firestore.client
asyncListener:asyncListener
internalListener:internalListener];
}
/** Converts the public API options object to the internal options object. */
- (FSTListenOptions *)internalOptions:(nullable FIRDocumentListenOptions *)options {
return
[[FSTListenOptions alloc] initWithIncludeQueryMetadataChanges:options.includeMetadataChanges
includeDocumentMetadataChanges:options.includeMetadataChanges
waitForSyncWhenOnline:NO];
}
@end
NS_ASSUME_NONNULL_END