FIRQuerySnapshot.mm 4.6 KB

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