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