aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source/API/FIRDocumentSnapshot.m
diff options
context:
space:
mode:
authorGravatar Sebastian Schmidt <mrschmidt@google.com>2017-12-11 17:34:55 -0800
committerGravatar GitHub <noreply@github.com>2017-12-11 17:34:55 -0800
commit95b0761721f5e2e89422f31720abc45f3bd03fd1 (patch)
treef95a5d9651d68432132841dc4f261a01965c1951 /Firestore/Source/API/FIRDocumentSnapshot.m
parent03c0e9882a87f8b5ac0699d64e7d17f940a4796a (diff)
parent601c13b0fbdcd21e80b6f0d26fc26fc662417fcc (diff)
Merge pull request #553 from firebase/mrschmidt-nullable
Making DocumentSnapshot.data() nullable
Diffstat (limited to 'Firestore/Source/API/FIRDocumentSnapshot.m')
-rw-r--r--Firestore/Source/API/FIRDocumentSnapshot.m57
1 files changed, 41 insertions, 16 deletions
diff --git a/Firestore/Source/API/FIRDocumentSnapshot.m b/Firestore/Source/API/FIRDocumentSnapshot.m
index c4e3040..36430ac 100644
--- a/Firestore/Source/API/FIRDocumentSnapshot.m
+++ b/Firestore/Source/API/FIRDocumentSnapshot.m
@@ -26,6 +26,7 @@
#import "Firestore/Source/Model/FSTDocumentKey.h"
#import "Firestore/Source/Model/FSTFieldValue.h"
#import "Firestore/Source/Model/FSTPath.h"
+#import "Firestore/Source/Util/FSTAssert.h"
#import "Firestore/Source/Util/FSTUsageValidation.h"
NS_ASSUME_NONNULL_BEGIN
@@ -100,23 +101,15 @@ NS_ASSUME_NONNULL_BEGIN
return _cachedMetadata;
}
-- (NSDictionary<NSString *, id> *)data {
+- (nullable NSDictionary<NSString *, id> *)data {
return [self dataWithOptions:[FIRSnapshotOptions defaultOptions]];
}
-- (NSDictionary<NSString *, id> *)dataWithOptions:(FIRSnapshotOptions *)options {
- FSTDocument *document = self.internalDocument;
-
- if (!document) {
- FSTThrowInvalidUsage(
- @"NonExistentDocumentException",
- @"Document '%@' doesn't exist. "
- @"Check document.exists to make sure the document exists before calling document.data.",
- self.internalKey);
- }
-
- return [self convertedObject:[self.internalDocument data]
- options:[FSTFieldValueOptions optionsForSnapshotOptions:options]];
+- (nullable NSDictionary<NSString *, id> *)dataWithOptions:(FIRSnapshotOptions *)options {
+ return self.internalDocument == nil
+ ? nil
+ : [self convertedObject:[self.internalDocument data]
+ options:[FSTFieldValueOptions optionsForSnapshotOptions:options]];
}
- (nullable id)valueForField:(id)field {
@@ -135,8 +128,10 @@ NS_ASSUME_NONNULL_BEGIN
}
FSTFieldValue *fieldValue = [[self.internalDocument data] valueForPath:fieldPath.internalValue];
- return [self convertedValue:fieldValue
- options:[FSTFieldValueOptions optionsForSnapshotOptions:options]];
+ return fieldValue == nil
+ ? nil
+ : [self convertedValue:fieldValue
+ options:[FSTFieldValueOptions optionsForSnapshotOptions:options]];
}
- (nullable id)objectForKeyedSubscript:(id)key {
@@ -190,4 +185,34 @@ NS_ASSUME_NONNULL_BEGIN
@end
+@interface FIRQueryDocumentSnapshot ()
+
+- (instancetype)initWithFirestore:(FIRFirestore *)firestore
+ documentKey:(FSTDocumentKey *)documentKey
+ document:(FSTDocument *)document
+ fromCache:(BOOL)fromCache NS_DESIGNATED_INITIALIZER;
+
+@end
+
+@implementation FIRQueryDocumentSnapshot
+
+- (instancetype)initWithFirestore:(FIRFirestore *)firestore
+ documentKey:(FSTDocumentKey *)documentKey
+ document:(FSTDocument *)document
+ fromCache:(BOOL)fromCache {
+ self = [super initWithFirestore:firestore
+ documentKey:documentKey
+ document:document
+ fromCache:fromCache];
+ return self;
+}
+
+- (NSDictionary<NSString *, id> *)data {
+ NSDictionary<NSString *, id> *data = [super data];
+ FSTAssert(data, @"Document in a QueryDocumentSnapshot should exist");
+ return data;
+}
+
+@end
+
NS_ASSUME_NONNULL_END