FIRQuerySnapshot.mm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include <utility>
  17. #import "Firestore/Source/API/FIRQuerySnapshot+Internal.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/FIRFirestore+Internal.h"
  22. #import "Firestore/Source/API/FIRQuery+Internal.h"
  23. #import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
  24. #import "Firestore/Source/Core/FSTQuery.h"
  25. #import "Firestore/Source/Model/FSTDocument.h"
  26. #import "Firestore/Source/Util/FSTUsageValidation.h"
  27. #include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
  28. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  29. #include "Firestore/core/src/firebase/firestore/util/delayed_constructor.h"
  30. using firebase::firestore::api::Firestore;
  31. using firebase::firestore::api::QuerySnapshot;
  32. using firebase::firestore::core::ViewSnapshot;
  33. using firebase::firestore::util::DelayedConstructor;
  34. NS_ASSUME_NONNULL_BEGIN
  35. @implementation FIRQuerySnapshot {
  36. DelayedConstructor<QuerySnapshot> _snapshot;
  37. FIRSnapshotMetadata *_cached_metadata;
  38. // Cached value of the documents property.
  39. NSArray<FIRQueryDocumentSnapshot *> *_documents;
  40. // Cached value of the documentChanges property.
  41. NSArray<FIRDocumentChange *> *_documentChanges;
  42. BOOL _documentChangesIncludeMetadataChanges;
  43. }
  44. - (instancetype)initWithSnapshot:(QuerySnapshot &&)snapshot {
  45. if (self = [super init]) {
  46. _snapshot.Init(std::move(snapshot));
  47. }
  48. return self;
  49. }
  50. - (instancetype)initWithFirestore:(Firestore *)firestore
  51. originalQuery:(FSTQuery *)query
  52. snapshot:(ViewSnapshot &&)snapshot
  53. metadata:(SnapshotMetadata)metadata {
  54. QuerySnapshot wrapped(firestore, query, std::move(snapshot), std::move(metadata));
  55. return [self initWithSnapshot:std::move(wrapped)];
  56. }
  57. // NSObject Methods
  58. - (BOOL)isEqual:(nullable id)other {
  59. if (![other isKindOfClass:[FIRQuerySnapshot class]]) return NO;
  60. FIRQuerySnapshot *otherSnapshot = other;
  61. return *_snapshot == *(otherSnapshot->_snapshot);
  62. }
  63. - (NSUInteger)hash {
  64. return _snapshot->Hash();
  65. }
  66. - (FIRQuery *)query {
  67. FIRFirestore *firestore = [FIRFirestore recoverFromFirestore:_snapshot->firestore()];
  68. return [FIRQuery referenceWithQuery:_snapshot->internal_query() firestore:firestore];
  69. }
  70. - (FIRSnapshotMetadata *)metadata {
  71. if (!_cached_metadata) {
  72. _cached_metadata = [[FIRSnapshotMetadata alloc] initWithMetadata:_snapshot->metadata()];
  73. }
  74. return _cached_metadata;
  75. }
  76. @dynamic empty;
  77. - (BOOL)isEmpty {
  78. return _snapshot->empty();
  79. }
  80. // This property is exposed as an NSInteger instead of an NSUInteger since (as of Xcode 8.1)
  81. // Swift bridges NSUInteger as UInt, and we want to avoid forcing Swift users to cast their ints
  82. // where we can. See cr/146959032 for additional context.
  83. - (NSInteger)count {
  84. return static_cast<NSInteger>(_snapshot->size());
  85. }
  86. - (NSArray<FIRQueryDocumentSnapshot *> *)documents {
  87. if (!_documents) {
  88. NSMutableArray<FIRQueryDocumentSnapshot *> *result = [NSMutableArray array];
  89. _snapshot->ForEachDocument([&result](DocumentSnapshot snapshot) {
  90. [result addObject:[[FIRQueryDocumentSnapshot alloc] initWithSnapshot:std::move(snapshot)]];
  91. });
  92. _documents = result;
  93. }
  94. return _documents;
  95. }
  96. - (NSArray<FIRDocumentChange *> *)documentChanges {
  97. return [self documentChangesWithIncludeMetadataChanges:NO];
  98. }
  99. - (NSArray<FIRDocumentChange *> *)documentChangesWithIncludeMetadataChanges:
  100. (BOOL)includeMetadataChanges {
  101. if (includeMetadataChanges && _snapshot->view_snapshot().excludes_metadata_changes()) {
  102. FSTThrowInvalidArgument(
  103. @"To include metadata changes with your document changes, you must call "
  104. @"addSnapshotListener(includeMetadataChanges: true).");
  105. }
  106. if (!_documentChanges || _documentChangesIncludeMetadataChanges != includeMetadataChanges) {
  107. _documentChanges = [FIRDocumentChange documentChangesForSnapshot:_snapshot->view_snapshot()
  108. includeMetadataChanges:includeMetadataChanges
  109. firestore:_snapshot->firestore()];
  110. _documentChangesIncludeMetadataChanges = includeMetadataChanges;
  111. }
  112. return _documentChanges;
  113. }
  114. @end
  115. NS_ASSUME_NONNULL_END