FIRQuerySnapshot.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/FSTAssert.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. }
  54. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  55. originalQuery:(FSTQuery *)query
  56. snapshot:(FSTViewSnapshot *)snapshot
  57. metadata:(FIRSnapshotMetadata *)metadata {
  58. if (self = [super init]) {
  59. _firestore = firestore;
  60. _originalQuery = query;
  61. _snapshot = snapshot;
  62. _metadata = metadata;
  63. }
  64. return self;
  65. }
  66. // NSObject Methods
  67. - (BOOL)isEqual:(nullable id)other {
  68. if (other == self) return YES;
  69. if (![[other class] isEqual:[self class]]) return NO;
  70. return [self isEqualToSnapshot:other];
  71. }
  72. - (BOOL)isEqualToSnapshot:(nullable FIRQuerySnapshot *)snapshot {
  73. if (self == snapshot) return YES;
  74. if (snapshot == nil) return NO;
  75. if (self.firestore != snapshot.firestore && ![self.firestore isEqual:snapshot.firestore])
  76. return NO;
  77. if (self.originalQuery != snapshot.originalQuery &&
  78. ![self.originalQuery isEqual:snapshot.originalQuery])
  79. return NO;
  80. if (self.snapshot != snapshot.snapshot && ![self.snapshot isEqual:snapshot.snapshot]) return NO;
  81. if (self.metadata != snapshot.metadata && ![self.metadata isEqual:snapshot.metadata]) return NO;
  82. return YES;
  83. }
  84. - (NSUInteger)hash {
  85. NSUInteger hash = [self.firestore hash];
  86. hash = hash * 31u + [self.originalQuery hash];
  87. hash = hash * 31u + [self.snapshot hash];
  88. hash = hash * 31u + [self.metadata hash];
  89. return hash;
  90. }
  91. @dynamic empty;
  92. - (FIRQuery *)query {
  93. return [FIRQuery referenceWithQuery:self.originalQuery firestore:self.firestore];
  94. }
  95. - (BOOL)isEmpty {
  96. return self.snapshot.documents.isEmpty;
  97. }
  98. // This property is exposed as an NSInteger instead of an NSUInteger since (as of Xcode 8.1)
  99. // Swift bridges NSUInteger as UInt, and we want to avoid forcing Swift users to cast their ints
  100. // where we can. See cr/146959032 for additional context.
  101. - (NSInteger)count {
  102. return self.snapshot.documents.count;
  103. }
  104. - (NSArray<FIRQueryDocumentSnapshot *> *)documents {
  105. if (!_documents) {
  106. FSTDocumentSet *documentSet = self.snapshot.documents;
  107. FIRFirestore *firestore = self.firestore;
  108. BOOL fromCache = self.metadata.fromCache;
  109. NSMutableArray<FIRQueryDocumentSnapshot *> *result = [NSMutableArray array];
  110. for (FSTDocument *document in documentSet.documentEnumerator) {
  111. [result addObject:[FIRQueryDocumentSnapshot snapshotWithFirestore:firestore
  112. documentKey:document.key
  113. document:document
  114. fromCache:fromCache]];
  115. }
  116. _documents = result;
  117. }
  118. return _documents;
  119. }
  120. - (NSArray<FIRDocumentChange *> *)documentChanges {
  121. if (!_documentChanges) {
  122. _documentChanges =
  123. [FIRDocumentChange documentChangesForSnapshot:self.snapshot firestore:self.firestore];
  124. }
  125. return _documentChanges;
  126. }
  127. @end
  128. NS_ASSUME_NONNULL_END