FIRQuerySnapshot.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "FIRSnapshotMetadata.h"
  18. #import "Firestore/Source/API/FIRDocumentChange+Internal.h"
  19. #import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
  20. #import "Firestore/Source/API/FIRQuery+Internal.h"
  21. #import "Firestore/Source/Core/FSTQuery.h"
  22. #import "Firestore/Source/Core/FSTViewSnapshot.h"
  23. #import "Firestore/Source/Model/FSTDocument.h"
  24. #import "Firestore/Source/Model/FSTDocumentSet.h"
  25. #import "Firestore/Source/Util/FSTAssert.h"
  26. NS_ASSUME_NONNULL_BEGIN
  27. @interface FIRQuerySnapshot ()
  28. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  29. originalQuery:(FSTQuery *)query
  30. snapshot:(FSTViewSnapshot *)snapshot
  31. metadata:(FIRSnapshotMetadata *)metadata;
  32. @property(nonatomic, strong, readonly) FIRFirestore *firestore;
  33. @property(nonatomic, strong, readonly) FSTQuery *originalQuery;
  34. @property(nonatomic, strong, readonly) FSTViewSnapshot *snapshot;
  35. @end
  36. @implementation FIRQuerySnapshot (Internal)
  37. + (instancetype)snapshotWithFirestore:(FIRFirestore *)firestore
  38. originalQuery:(FSTQuery *)query
  39. snapshot:(FSTViewSnapshot *)snapshot
  40. metadata:(FIRSnapshotMetadata *)metadata {
  41. return [[FIRQuerySnapshot alloc] initWithFirestore:firestore
  42. originalQuery:query
  43. snapshot:snapshot
  44. metadata:metadata];
  45. }
  46. @end
  47. @implementation FIRQuerySnapshot {
  48. // Cached value of the documents property.
  49. NSArray<FIRDocumentSnapshot *> *_documents;
  50. // Cached value of the documentChanges property.
  51. NSArray<FIRDocumentChange *> *_documentChanges;
  52. }
  53. - (instancetype)initWithFirestore:(FIRFirestore *)firestore
  54. originalQuery:(FSTQuery *)query
  55. snapshot:(FSTViewSnapshot *)snapshot
  56. metadata:(FIRSnapshotMetadata *)metadata {
  57. if (self = [super init]) {
  58. _firestore = firestore;
  59. _originalQuery = query;
  60. _snapshot = snapshot;
  61. _metadata = metadata;
  62. }
  63. return self;
  64. }
  65. @dynamic empty;
  66. - (FIRQuery *)query {
  67. return [FIRQuery referenceWithQuery:self.originalQuery firestore:self.firestore];
  68. }
  69. - (BOOL)isEmpty {
  70. return self.snapshot.documents.isEmpty;
  71. }
  72. // This property is exposed as an NSInteger instead of an NSUInteger since (as of Xcode 8.1)
  73. // Swift bridges NSUInteger as UInt, and we want to avoid forcing Swift users to cast their ints
  74. // where we can. See cr/146959032 for additional context.
  75. - (NSInteger)count {
  76. return self.snapshot.documents.count;
  77. }
  78. - (NSArray<FIRDocumentSnapshot *> *)documents {
  79. if (!_documents) {
  80. FSTDocumentSet *documentSet = self.snapshot.documents;
  81. FIRFirestore *firestore = self.firestore;
  82. BOOL fromCache = self.metadata.fromCache;
  83. NSMutableArray<FIRDocumentSnapshot *> *result = [NSMutableArray array];
  84. for (FSTDocument *document in documentSet.documentEnumerator) {
  85. [result addObject:[FIRDocumentSnapshot snapshotWithFirestore:firestore
  86. documentKey:document.key
  87. document:document
  88. fromCache:fromCache]];
  89. }
  90. _documents = result;
  91. }
  92. return _documents;
  93. }
  94. - (NSArray<FIRDocumentChange *> *)documentChanges {
  95. if (!_documentChanges) {
  96. _documentChanges =
  97. [FIRDocumentChange documentChangesForSnapshot:self.snapshot firestore:self.firestore];
  98. }
  99. return _documentChanges;
  100. }
  101. @end
  102. NS_ASSUME_NONNULL_END