FIRQuerySnapshot.mm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. return [self.firestore isEqual:snapshot.firestore] &&
  76. [self.originalQuery isEqual:snapshot.originalQuery] &&
  77. [self.snapshot isEqual:snapshot.snapshot] && [self.metadata isEqual:snapshot.metadata];
  78. }
  79. - (NSUInteger)hash {
  80. NSUInteger hash = [self.firestore hash];
  81. hash = hash * 31u + [self.originalQuery hash];
  82. hash = hash * 31u + [self.snapshot hash];
  83. hash = hash * 31u + [self.metadata hash];
  84. return hash;
  85. }
  86. @dynamic empty;
  87. - (FIRQuery *)query {
  88. return [FIRQuery referenceWithQuery:self.originalQuery firestore:self.firestore];
  89. }
  90. - (BOOL)isEmpty {
  91. return self.snapshot.documents.isEmpty;
  92. }
  93. // This property is exposed as an NSInteger instead of an NSUInteger since (as of Xcode 8.1)
  94. // Swift bridges NSUInteger as UInt, and we want to avoid forcing Swift users to cast their ints
  95. // where we can. See cr/146959032 for additional context.
  96. - (NSInteger)count {
  97. return self.snapshot.documents.count;
  98. }
  99. - (NSArray<FIRQueryDocumentSnapshot *> *)documents {
  100. if (!_documents) {
  101. FSTDocumentSet *documentSet = self.snapshot.documents;
  102. FIRFirestore *firestore = self.firestore;
  103. BOOL fromCache = self.metadata.fromCache;
  104. NSMutableArray<FIRQueryDocumentSnapshot *> *result = [NSMutableArray array];
  105. for (FSTDocument *document in documentSet.documentEnumerator) {
  106. [result addObject:[FIRQueryDocumentSnapshot snapshotWithFirestore:firestore
  107. documentKey:document.key
  108. document:document
  109. fromCache:fromCache]];
  110. }
  111. _documents = result;
  112. }
  113. return _documents;
  114. }
  115. - (NSArray<FIRDocumentChange *> *)documentChanges {
  116. if (!_documentChanges) {
  117. _documentChanges =
  118. [FIRDocumentChange documentChangesForSnapshot:self.snapshot firestore:self.firestore];
  119. }
  120. return _documentChanges;
  121. }
  122. @end
  123. NS_ASSUME_NONNULL_END