FIRDocumentSnapshot.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "FIRDocumentSnapshot.h"
  17. #import "Firestore/Source/API/FIRDocumentReference+Internal.h"
  18. #import "Firestore/Source/API/FIRFieldPath+Internal.h"
  19. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  20. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  21. #import "Firestore/Source/API/FIRSnapshotOptions+Internal.h"
  22. #import "Firestore/Source/Model/FSTDatabaseID.h"
  23. #import "Firestore/Source/Model/FSTDocument.h"
  24. #import "Firestore/Source/Model/FSTDocumentKey.h"
  25. #import "Firestore/Source/Model/FSTFieldValue.h"
  26. #import "Firestore/Source/Model/FSTPath.h"
  27. #import "Firestore/Source/Util/FSTAssert.h"
  28. #import "Firestore/Source/Util/FSTUsageValidation.h"
  29. NS_ASSUME_NONNULL_BEGIN
  30. @interface FIRDocumentSnapshot ()
  31. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  32. documentKey:(FSTDocumentKey *)documentKey
  33. document:(nullable FSTDocument *)document
  34. fromCache:(BOOL)fromCache NS_DESIGNATED_INITIALIZER;
  35. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  36. @property(nonatomic, strong, readonly) FSTDocumentKey *internalKey;
  37. @property(nonatomic, strong, readonly, nullable) FSTDocument *internalDocument;
  38. @property(nonatomic, assign, readonly) BOOL fromCache;
  39. @end
  40. @implementation FIRDocumentSnapshot (Internal)
  41. + (instancetype)snapshotWithFirestore:(FIRFirestore *)firestore
  42. documentKey:(FSTDocumentKey *)documentKey
  43. document:(nullable FSTDocument *)document
  44. fromCache:(BOOL)fromCache {
  45. return [[[self class] alloc] initWithFirestore:firestore
  46. documentKey:documentKey
  47. document:document
  48. fromCache:fromCache];
  49. }
  50. @end
  51. @implementation FIRDocumentSnapshot {
  52. FIRSnapshotMetadata *_cachedMetadata;
  53. }
  54. @dynamic metadata;
  55. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  56. documentKey:(FSTDocumentKey *)documentKey
  57. document:(nullable FSTDocument *)document
  58. fromCache:(BOOL)fromCache {
  59. if (self = [super init]) {
  60. _firestore = firestore;
  61. _internalKey = documentKey;
  62. _internalDocument = document;
  63. _fromCache = fromCache;
  64. }
  65. return self;
  66. }
  67. // NSObject Methods
  68. - (BOOL)isEqual:(nullable id)other {
  69. if (other == self) return YES;
  70. // self class could be FIRDocumentSnapshot or subtype. So we compare with base type explicitly.
  71. if (![other isKindOfClass:[FIRDocumentSnapshot class]]) return NO;
  72. return [self isEqualToSnapshot:other];
  73. }
  74. - (BOOL)isEqualToSnapshot:(nullable FIRDocumentSnapshot *)snapshot {
  75. if (self == snapshot) return YES;
  76. if (snapshot == nil) return NO;
  77. if (self.firestore != snapshot.firestore && ![self.firestore isEqual:snapshot.firestore])
  78. return NO;
  79. if (self.internalKey != snapshot.internalKey && ![self.internalKey isEqual:snapshot.internalKey])
  80. return NO;
  81. if (self.internalDocument != snapshot.internalDocument &&
  82. ![self.internalDocument isEqual:snapshot.internalDocument])
  83. return NO;
  84. if (self.fromCache != snapshot.fromCache) return NO;
  85. return YES;
  86. }
  87. - (NSUInteger)hash {
  88. NSUInteger hash = [self.firestore hash];
  89. hash = hash * 31u + [self.internalKey hash];
  90. hash = hash * 31u + [self.internalDocument hash];
  91. hash = hash * 31u + (self.fromCache ? 1 : 0);
  92. return hash;
  93. }
  94. @dynamic exists;
  95. - (BOOL)exists {
  96. return _internalDocument != nil;
  97. }
  98. - (FIRDocumentReference *)reference {
  99. return [FIRDocumentReference referenceWithKey:self.internalKey firestore:self.firestore];
  100. }
  101. - (NSString *)documentID {
  102. return [self.internalKey.path lastSegment];
  103. }
  104. - (FIRSnapshotMetadata *)metadata {
  105. if (!_cachedMetadata) {
  106. _cachedMetadata = [FIRSnapshotMetadata
  107. snapshotMetadataWithPendingWrites:self.internalDocument.hasLocalMutations
  108. fromCache:self.fromCache];
  109. }
  110. return _cachedMetadata;
  111. }
  112. - (nullable NSDictionary<NSString *, id> *)data {
  113. return [self dataWithOptions:[FIRSnapshotOptions defaultOptions]];
  114. }
  115. - (nullable NSDictionary<NSString *, id> *)dataWithOptions:(FIRSnapshotOptions *)options {
  116. return self.internalDocument == nil
  117. ? nil
  118. : [self convertedObject:[self.internalDocument data]
  119. options:[FSTFieldValueOptions optionsForSnapshotOptions:options]];
  120. }
  121. - (nullable id)valueForField:(id)field {
  122. return [self valueForField:field options:[FIRSnapshotOptions defaultOptions]];
  123. }
  124. - (nullable id)valueForField:(id)field options:(FIRSnapshotOptions *)options {
  125. FIRFieldPath *fieldPath;
  126. if ([field isKindOfClass:[NSString class]]) {
  127. fieldPath = [FIRFieldPath pathWithDotSeparatedString:field];
  128. } else if ([field isKindOfClass:[FIRFieldPath class]]) {
  129. fieldPath = field;
  130. } else {
  131. FSTThrowInvalidArgument(@"Subscript key must be an NSString or FIRFieldPath.");
  132. }
  133. FSTFieldValue *fieldValue = [[self.internalDocument data] valueForPath:fieldPath.internalValue];
  134. return fieldValue == nil
  135. ? nil
  136. : [self convertedValue:fieldValue
  137. options:[FSTFieldValueOptions optionsForSnapshotOptions:options]];
  138. }
  139. - (nullable id)objectForKeyedSubscript:(id)key {
  140. return [self valueForField:key];
  141. }
  142. - (id)convertedValue:(FSTFieldValue *)value options:(FSTFieldValueOptions *)options {
  143. if ([value isKindOfClass:[FSTObjectValue class]]) {
  144. return [self convertedObject:(FSTObjectValue *)value options:options];
  145. } else if ([value isKindOfClass:[FSTArrayValue class]]) {
  146. return [self convertedArray:(FSTArrayValue *)value options:options];
  147. } else if ([value isKindOfClass:[FSTReferenceValue class]]) {
  148. FSTReferenceValue *ref = (FSTReferenceValue *)value;
  149. FSTDatabaseID *refDatabase = ref.databaseID;
  150. FSTDatabaseID *database = self.firestore.databaseID;
  151. if (![refDatabase isEqualToDatabaseId:database]) {
  152. // TODO(b/32073923): Log this as a proper warning.
  153. NSLog(
  154. @"WARNING: Document %@ contains a document reference within a different database "
  155. "(%@/%@) which is not supported. It will be treated as a reference within the "
  156. "current database (%@/%@) instead.",
  157. self.reference.path, refDatabase.projectID, refDatabase.databaseID, database.projectID,
  158. database.databaseID);
  159. }
  160. return [FIRDocumentReference referenceWithKey:[ref valueWithOptions:options]
  161. firestore:self.firestore];
  162. } else {
  163. return [value valueWithOptions:options];
  164. }
  165. }
  166. - (NSDictionary<NSString *, id> *)convertedObject:(FSTObjectValue *)objectValue
  167. options:(FSTFieldValueOptions *)options {
  168. NSMutableDictionary *result = [NSMutableDictionary dictionary];
  169. [objectValue.internalValue
  170. enumerateKeysAndObjectsUsingBlock:^(NSString *key, FSTFieldValue *value, BOOL *stop) {
  171. result[key] = [self convertedValue:value options:options];
  172. }];
  173. return result;
  174. }
  175. - (NSArray<id> *)convertedArray:(FSTArrayValue *)arrayValue
  176. options:(FSTFieldValueOptions *)options {
  177. NSArray<FSTFieldValue *> *internalValue = arrayValue.internalValue;
  178. NSMutableArray *result = [NSMutableArray arrayWithCapacity:internalValue.count];
  179. [internalValue enumerateObjectsUsingBlock:^(id value, NSUInteger idx, BOOL *stop) {
  180. [result addObject:[self convertedValue:value options:options]];
  181. }];
  182. return result;
  183. }
  184. @end
  185. @interface FIRQueryDocumentSnapshot ()
  186. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  187. documentKey:(FSTDocumentKey *)documentKey
  188. document:(FSTDocument *)document
  189. fromCache:(BOOL)fromCache NS_DESIGNATED_INITIALIZER;
  190. @end
  191. @implementation FIRQueryDocumentSnapshot
  192. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  193. documentKey:(FSTDocumentKey *)documentKey
  194. document:(FSTDocument *)document
  195. fromCache:(BOOL)fromCache {
  196. self = [super initWithFirestore:firestore
  197. documentKey:documentKey
  198. document:document
  199. fromCache:fromCache];
  200. return self;
  201. }
  202. - (NSDictionary<NSString *, id> *)data {
  203. NSDictionary<NSString *, id> *data = [super data];
  204. FSTAssert(data, @"Document in a QueryDocumentSnapshot should exist");
  205. return data;
  206. }
  207. - (NSDictionary<NSString *, id> *)dataWithOptions:(FIRSnapshotOptions *)options {
  208. NSDictionary<NSString *, id> *data = [super dataWithOptions:options];
  209. FSTAssert(data, @"Document in a QueryDocumentSnapshot should exist");
  210. return data;
  211. }
  212. @end
  213. NS_ASSUME_NONNULL_END