FIRQuerySnapshot.mm 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/FIRQuerySnapshot+Internal.h"
  17. #import "FIRFirestore.h"
  18. #import "FIRSnapshotMetadata.h"
  19. #import "Firestore/Source/API/FIRDocumentChange+Internal.h"
  20. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  21. #import "Firestore/Source/API/FIRQuery+Internal.h"
  22. #import "Firestore/Source/Core/FSTQuery.h"
  23. #import "Firestore/Source/Core/FSTViewSnapshot.h"
  24. #import "Firestore/Source/Model/FSTDocument.h"
  25. #import "Firestore/Source/Model/FSTDocumentSet.h"
  26. #import "Firestore/Source/Util/FSTUsageValidation.h"
  27. NS_ASSUME_NONNULL_BEGIN
  28. @interface FIRQuerySnapshot ()
  29. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  30. originalQuery:(FSTQuery *)query
  31. snapshot:(FSTViewSnapshot *)snapshot
  32. metadata:(FIRSnapshotMetadata *)metadata;
  33. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  34. @property(nonatomic, strong, readonly) FSTQuery *originalQuery;
  35. @property(nonatomic, strong, readonly) FSTViewSnapshot *snapshot;
  36. @end
  37. @implementation FIRQuerySnapshot (Internal)
  38. + (instancetype)snapshotWithFirestore:(FIRFirestore *)firestore
  39. originalQuery:(FSTQuery *)query
  40. snapshot:(FSTViewSnapshot *)snapshot
  41. metadata:(FIRSnapshotMetadata *)metadata {
  42. return [[FIRQuerySnapshot alloc] initWithFirestore:firestore
  43. originalQuery:query
  44. snapshot:snapshot
  45. metadata:metadata];
  46. }
  47. @end
  48. @implementation FIRQuerySnapshot {
  49. // Cached value of the documents property.
  50. NSArray<FIRQueryDocumentSnapshot *> *_documents;
  51. // Cached value of the documentChanges property.
  52. NSArray<FIRDocumentChange *> *_documentChanges;
  53. BOOL _documentChangesIncludeMetadataChanges;
  54. }
  55. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  56. originalQuery:(FSTQuery *)query
  57. snapshot:(FSTViewSnapshot *)snapshot
  58. metadata:(FIRSnapshotMetadata *)metadata {
  59. if (self = [super init]) {
  60. _firestore = firestore;
  61. _originalQuery = query;
  62. _snapshot = snapshot;
  63. _metadata = metadata;
  64. _documentChangesIncludeMetadataChanges = NO;
  65. }
  66. return self;
  67. }
  68. // NSObject Methods
  69. - (BOOL)isEqual:(nullable id)other {
  70. if (other == self) return YES;
  71. if (![[other class] isEqual:[self class]]) return NO;
  72. return [self isEqualToSnapshot:other];
  73. }
  74. - (BOOL)isEqualToSnapshot:(nullable FIRQuerySnapshot *)snapshot {
  75. if (self == snapshot) return YES;
  76. if (snapshot == nil) return NO;
  77. return [self.firestore isEqual:snapshot.firestore] &&
  78. [self.originalQuery isEqual:snapshot.originalQuery] &&
  79. [self.snapshot isEqual:snapshot.snapshot] && [self.metadata isEqual:snapshot.metadata];
  80. }
  81. - (NSUInteger)hash {
  82. NSUInteger hash = [self.firestore hash];
  83. hash = hash * 31u + [self.originalQuery hash];
  84. hash = hash * 31u + [self.snapshot hash];
  85. hash = hash * 31u + [self.metadata hash];
  86. return hash;
  87. }
  88. @dynamic empty;
  89. - (FIRQuery *)query {
  90. return [FIRQuery referenceWithQuery:self.originalQuery firestore:self.firestore];
  91. }
  92. - (BOOL)isEmpty {
  93. return self.snapshot.documents.isEmpty;
  94. }
  95. // This property is exposed as an NSInteger instead of an NSUInteger since (as of Xcode 8.1)
  96. // Swift bridges NSUInteger as UInt, and we want to avoid forcing Swift users to cast their ints
  97. // where we can. See cr/146959032 for additional context.
  98. - (NSInteger)count {
  99. return self.snapshot.documents.count;
  100. }
  101. - (NSArray<FIRQueryDocumentSnapshot *> *)documents {
  102. if (!_documents) {
  103. FSTDocumentSet *documentSet = self.snapshot.documents;
  104. FIRFirestore *firestore = self.firestore;
  105. BOOL fromCache = self.metadata.fromCache;
  106. NSMutableArray<FIRQueryDocumentSnapshot *> *result = [NSMutableArray array];
  107. for (FSTDocument *document in documentSet.documentEnumerator) {
  108. [result
  109. addObject:[FIRQueryDocumentSnapshot
  110. snapshotWithFirestore:firestore
  111. documentKey:document.key
  112. document:document
  113. fromCache:fromCache
  114. hasPendingWrites:self.snapshot.mutatedKeys.contains(document.key)]];
  115. }
  116. _documents = result;
  117. }
  118. return _documents;
  119. }
  120. - (NSArray<FIRDocumentChange *> *)documentChanges {
  121. return [self documentChangesWithIncludeMetadataChanges:NO];
  122. }
  123. - (NSArray<FIRDocumentChange *> *)documentChangesWithIncludeMetadataChanges:
  124. (BOOL)includeMetadataChanges {
  125. if (includeMetadataChanges && self.snapshot.excludesMetadataChanges) {
  126. FSTThrowInvalidArgument(
  127. @"To include metadata changes with your document changes, you must call "
  128. @"addSnapshotListener(includeMetadataChanges: true).");
  129. }
  130. if (!_documentChanges || _documentChangesIncludeMetadataChanges != includeMetadataChanges) {
  131. _documentChanges = [FIRDocumentChange documentChangesForSnapshot:self.snapshot
  132. includeMetadataChanges:includeMetadataChanges
  133. firestore:self.firestore];
  134. _documentChangesIncludeMetadataChanges = includeMetadataChanges;
  135. }
  136. return _documentChanges;
  137. }
  138. @end
  139. NS_ASSUME_NONNULL_END