Files
firebase-ios-sdk/Firestore/Source/API/FIRQuerySnapshot.mm
T

142 lines
4.7 KiB
Plaintext
Raw Normal View History

2017-10-03 08:55:22 -07:00
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2019-03-01 14:16:41 -05:00
#include <utility>
#import "Firestore/Source/API/FIRQuerySnapshot+Internal.h"
2017-10-03 08:55:22 -07:00
#import "FIRSnapshotMetadata.h"
#import "Firestore/Source/API/FIRDocumentChange+Internal.h"
#import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
#import "Firestore/Source/API/FIRFirestore+Internal.h"
#import "Firestore/Source/API/FIRQuery+Internal.h"
2019-03-20 08:04:57 -07:00
#import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
2017-10-03 08:55:22 -07:00
2019-03-01 14:16:41 -05:00
#include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
2019-03-25 11:04:21 -07:00
#include "Firestore/core/src/firebase/firestore/model/document_set.h"
#include "Firestore/core/src/firebase/firestore/util/delayed_constructor.h"
#include "Firestore/core/src/firebase/firestore/util/exception.h"
2019-03-01 14:16:41 -05:00
using firebase::firestore::api::DocumentChange;
using firebase::firestore::api::DocumentSnapshot;
using firebase::firestore::api::Firestore;
2019-03-20 08:04:57 -07:00
using firebase::firestore::api::QuerySnapshot;
using firebase::firestore::api::SnapshotMetadata;
2019-03-01 14:16:41 -05:00
using firebase::firestore::core::ViewSnapshot;
2019-03-25 11:04:21 -07:00
using firebase::firestore::util::DelayedConstructor;
using firebase::firestore::util::ThrowInvalidArgument;
2019-03-01 14:16:41 -05:00
2017-10-03 08:55:22 -07:00
NS_ASSUME_NONNULL_BEGIN
@implementation FIRQuerySnapshot {
2019-03-25 11:04:21 -07:00
DelayedConstructor<QuerySnapshot> _snapshot;
2019-03-20 08:04:57 -07:00
FIRSnapshotMetadata *_cached_metadata;
2017-10-03 08:55:22 -07:00
// Cached value of the documents property.
2017-12-10 14:40:22 -08:00
NSArray<FIRQueryDocumentSnapshot *> *_documents;
2017-10-03 08:55:22 -07:00
// Cached value of the documentChanges property.
NSArray<FIRDocumentChange *> *_documentChanges;
BOOL _documentChangesIncludeMetadataChanges;
2017-10-03 08:55:22 -07:00
}
2019-03-20 08:04:57 -07:00
- (instancetype)initWithSnapshot:(QuerySnapshot &&)snapshot {
2017-10-03 08:55:22 -07:00
if (self = [super init]) {
2019-03-25 11:04:21 -07:00
_snapshot.Init(std::move(snapshot));
2017-10-03 08:55:22 -07:00
}
return self;
}
- (instancetype)initWithFirestore:(std::shared_ptr<Firestore>)firestore
2019-08-02 11:35:33 -07:00
originalQuery:(core::Query)query
2019-03-20 08:04:57 -07:00
snapshot:(ViewSnapshot &&)snapshot
metadata:(SnapshotMetadata)metadata {
2019-08-02 11:35:33 -07:00
QuerySnapshot wrapped(firestore, std::move(query), std::move(snapshot), std::move(metadata));
2019-03-20 08:04:57 -07:00
return [self initWithSnapshot:std::move(wrapped)];
2019-03-01 14:16:41 -05:00
}
2017-12-19 13:47:04 -05:00
// NSObject Methods
- (BOOL)isEqual:(nullable id)other {
2019-03-20 08:04:57 -07:00
if (![other isKindOfClass:[FIRQuerySnapshot class]]) return NO;
2017-12-19 13:47:04 -05:00
2019-03-20 08:04:57 -07:00
FIRQuerySnapshot *otherSnapshot = other;
2019-03-25 11:04:21 -07:00
return *_snapshot == *(otherSnapshot->_snapshot);
2017-12-19 13:47:04 -05:00
}
- (NSUInteger)hash {
2019-03-25 11:04:21 -07:00
return _snapshot->Hash();
2019-03-20 08:04:57 -07:00
}
- (FIRQuery *)query {
2019-05-14 14:04:35 -07:00
return [[FIRQuery alloc] initWithQuery:_snapshot->query()];
2019-03-20 08:04:57 -07:00
}
- (FIRSnapshotMetadata *)metadata {
if (!_cached_metadata) {
2019-03-25 11:04:21 -07:00
_cached_metadata = [[FIRSnapshotMetadata alloc] initWithMetadata:_snapshot->metadata()];
2019-03-20 08:04:57 -07:00
}
return _cached_metadata;
2017-12-19 13:47:04 -05:00
}
2017-10-03 08:55:22 -07:00
@dynamic empty;
- (BOOL)isEmpty {
2019-03-25 11:04:21 -07:00
return _snapshot->empty();
2017-10-03 08:55:22 -07:00
}
// This property is exposed as an NSInteger instead of an NSUInteger since (as of Xcode 8.1)
// Swift bridges NSUInteger as UInt, and we want to avoid forcing Swift users to cast their ints
// where we can. See cr/146959032 for additional context.
- (NSInteger)count {
2019-03-25 11:04:21 -07:00
return static_cast<NSInteger>(_snapshot->size());
2017-10-03 08:55:22 -07:00
}
2017-12-10 14:40:22 -08:00
- (NSArray<FIRQueryDocumentSnapshot *> *)documents {
2017-10-03 08:55:22 -07:00
if (!_documents) {
2017-12-10 14:40:22 -08:00
NSMutableArray<FIRQueryDocumentSnapshot *> *result = [NSMutableArray array];
2019-03-25 11:04:21 -07:00
_snapshot->ForEachDocument([&result](DocumentSnapshot snapshot) {
2019-03-20 08:04:57 -07:00
[result addObject:[[FIRQueryDocumentSnapshot alloc] initWithSnapshot:std::move(snapshot)]];
});
2017-10-03 08:55:22 -07:00
_documents = result;
}
return _documents;
}
- (NSArray<FIRDocumentChange *> *)documentChanges {
return [self documentChangesWithIncludeMetadataChanges:NO];
}
- (NSArray<FIRDocumentChange *> *)documentChangesWithIncludeMetadataChanges:
(BOOL)includeMetadataChanges {
if (!_documentChanges || _documentChangesIncludeMetadataChanges != includeMetadataChanges) {
2019-04-03 11:52:20 -07:00
NSMutableArray *documentChanges = [NSMutableArray array];
_snapshot->ForEachChange(
static_cast<bool>(includeMetadataChanges), [&documentChanges](DocumentChange change) {
[documentChanges
addObject:[[FIRDocumentChange alloc] initWithDocumentChange:std::move(change)]];
});
_documentChanges = documentChanges;
_documentChangesIncludeMetadataChanges = includeMetadataChanges;
2017-10-03 08:55:22 -07:00
}
return _documentChanges;
}
@end
NS_ASSUME_NONNULL_END