diff options
author | Gil <mcg@google.com> | 2018-04-15 16:56:43 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-15 16:56:43 -0700 |
commit | 4de2d80e4371e50419823961789424d40561f75a (patch) | |
tree | df0248786efd946233490508557d10faf2021554 /Firestore/Source | |
parent | 5368c9e22f9a6b427466e9422645d688953013c0 (diff) |
Replace `QueryListenOptions` with simple booleans (#1106)
* Replace `QueryListenOptions` with simple booleans
Instead of calling
addSnapshotListener(
options:QueryListenOptions.includeQueryMetadataChanges()
.includeDocumentMetadataChanges())
call
addSnapshotListener(includeMetadataChanges:true)
Also change `QuerySnapshot.documentChanges()` into a method which
optionally takes `includeMetadataChanges:true`. By default even when
listening to a query with `inlcudeMetadataChanges:true` metadata-only
document changes are suppressed because they're confusing.
* Revert QuerySnapshot.documentChanges back to a property
Add usage.
Diffstat (limited to 'Firestore/Source')
-rw-r--r-- | Firestore/Source/API/FIRDocumentChange+Internal.h | 1 | ||||
-rw-r--r-- | Firestore/Source/API/FIRDocumentChange.mm | 25 | ||||
-rw-r--r-- | Firestore/Source/API/FIRQuery.mm | 62 | ||||
-rw-r--r-- | Firestore/Source/API/FIRQuerySnapshot.mm | 15 | ||||
-rw-r--r-- | Firestore/Source/Public/FIRQuery.h | 51 | ||||
-rw-r--r-- | Firestore/Source/Public/FIRQuerySnapshot.h | 10 |
6 files changed, 63 insertions, 101 deletions
diff --git a/Firestore/Source/API/FIRDocumentChange+Internal.h b/Firestore/Source/API/FIRDocumentChange+Internal.h index 7c9723c..2aaced1 100644 --- a/Firestore/Source/API/FIRDocumentChange+Internal.h +++ b/Firestore/Source/API/FIRDocumentChange+Internal.h @@ -26,6 +26,7 @@ NS_ASSUME_NONNULL_BEGIN /** Calculates the array of FIRDocumentChange's based on the given FSTViewSnapshot. */ + (NSArray<FIRDocumentChange *> *)documentChangesForSnapshot:(FSTViewSnapshot *)snapshot + includeMetadataChanges:(BOOL)includeMetadataChanges firestore:(FIRFirestore *)firestore; @end diff --git a/Firestore/Source/API/FIRDocumentChange.mm b/Firestore/Source/API/FIRDocumentChange.mm index d1d9999..7bb24d2 100644 --- a/Firestore/Source/API/FIRDocumentChange.mm +++ b/Firestore/Source/API/FIRDocumentChange.mm @@ -50,9 +50,11 @@ NS_ASSUME_NONNULL_BEGIN } + (NSArray<FIRDocumentChange *> *)documentChangesForSnapshot:(FSTViewSnapshot *)snapshot + includeMetadataChanges:(BOOL)includeMetadataChanges firestore:(FIRFirestore *)firestore { if (snapshot.oldDocuments.isEmpty) { - // Special case the first snapshot because index calculation is easy and fast + // Special case the first snapshot because index calculation is easy and fast. Also all changes + // on the first snapshot are adds so there are also no metadata-only changes to filter out. FSTDocument *_Nullable lastDocument = nil; NSUInteger index = 0; NSMutableArray<FIRDocumentChange *> *changes = [NSMutableArray array]; @@ -79,6 +81,10 @@ NS_ASSUME_NONNULL_BEGIN FSTDocumentSet *indexTracker = snapshot.oldDocuments; NSMutableArray<FIRDocumentChange *> *changes = [NSMutableArray array]; for (FSTDocumentViewChange *change in snapshot.documentChanges) { + if (!includeMetadataChanges && change.type == FSTDocumentViewChangeTypeMetadata) { + continue; + } + FIRQueryDocumentSnapshot *document = [FIRQueryDocumentSnapshot snapshotWithFirestore:firestore documentKey:change.document.key @@ -124,6 +130,23 @@ NS_ASSUME_NONNULL_BEGIN return self; } +- (BOOL)isEqual:(nullable id)other { + if (other == self) return YES; + if (![other isKindOfClass:[FIRDocumentChange class]]) return NO; + + FIRDocumentChange *change = (FIRDocumentChange *)other; + return self.type == change.type && [self.document isEqual:change.document] && + self.oldIndex == change.oldIndex && self.newIndex == change.newIndex; +} + +- (NSUInteger)hash { + NSUInteger result = (NSUInteger)self.type; + result = result * 31u + [self.document hash]; + result = result * 31u + (NSUInteger)self.oldIndex; + result = result * 31u + (NSUInteger)self.newIndex; + return result; +} + @end NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/API/FIRQuery.mm b/Firestore/Source/API/FIRQuery.mm index 9cdc572..14dcaef 100644 --- a/Firestore/Source/API/FIRQuery.mm +++ b/Firestore/Source/API/FIRQuery.mm @@ -48,47 +48,6 @@ using firebase::firestore::model::ResourcePath; NS_ASSUME_NONNULL_BEGIN -@interface FIRQueryListenOptions () - -- (instancetype)initWithIncludeQueryMetadataChanges:(BOOL)includeQueryMetadataChanges - includeDocumentMetadataChanges:(BOOL)includeDocumentMetadataChanges - NS_DESIGNATED_INITIALIZER; - -@end - -@implementation FIRQueryListenOptions - -+ (instancetype)options { - return [[FIRQueryListenOptions alloc] init]; -} - -- (instancetype)initWithIncludeQueryMetadataChanges:(BOOL)includeQueryMetadataChanges - includeDocumentMetadataChanges:(BOOL)includeDocumentMetadataChanges { - if (self = [super init]) { - _includeQueryMetadataChanges = includeQueryMetadataChanges; - _includeDocumentMetadataChanges = includeDocumentMetadataChanges; - } - return self; -} - -- (instancetype)init { - return [self initWithIncludeQueryMetadataChanges:NO includeDocumentMetadataChanges:NO]; -} - -- (instancetype)includeQueryMetadataChanges:(BOOL)includeQueryMetadataChanges { - return [[FIRQueryListenOptions alloc] - initWithIncludeQueryMetadataChanges:includeQueryMetadataChanges - includeDocumentMetadataChanges:_includeDocumentMetadataChanges]; -} - -- (instancetype)includeDocumentMetadataChanges:(BOOL)includeDocumentMetadataChanges { - return [[FIRQueryListenOptions alloc] - initWithIncludeQueryMetadataChanges:_includeQueryMetadataChanges - includeDocumentMetadataChanges:includeDocumentMetadataChanges]; -} - -@end - @interface FIRQuery () @property(nonatomic, strong, readonly) FSTQuery *query; @end @@ -162,14 +121,14 @@ NS_ASSUME_NONNULL_BEGIN } - (id<FIRListenerRegistration>)addSnapshotListener:(FIRQuerySnapshotBlock)listener { - return [self addSnapshotListenerWithOptions:nil listener:listener]; + return [self addSnapshotListenerWithIncludeMetadataChanges:NO listener:listener]; } -- (id<FIRListenerRegistration>)addSnapshotListenerWithOptions: - (nullable FIRQueryListenOptions *)options - listener:(FIRQuerySnapshotBlock)listener { - return [self addSnapshotListenerInternalWithOptions:[self internalOptions:options] - listener:listener]; +- (id<FIRListenerRegistration>) +addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges + listener:(FIRQuerySnapshotBlock)listener { + auto options = [self internalOptionsForIncludeMetadataChanges:includeMetadataChanges]; + return [self addSnapshotListenerInternalWithOptions:options listener:listener]; } - (id<FIRListenerRegistration>) @@ -629,11 +588,10 @@ addSnapshotListenerInternalWithOptions:(FSTListenOptions *)internalOptions } /** Converts the public API options object to the internal options object. */ -- (FSTListenOptions *)internalOptions:(nullable FIRQueryListenOptions *)options { - return [[FSTListenOptions alloc] - initWithIncludeQueryMetadataChanges:options.includeQueryMetadataChanges - includeDocumentMetadataChanges:options.includeDocumentMetadataChanges - waitForSyncWhenOnline:NO]; +- (FSTListenOptions *)internalOptionsForIncludeMetadataChanges:(BOOL)includeMetadataChanges { + return [[FSTListenOptions alloc] initWithIncludeQueryMetadataChanges:includeMetadataChanges + includeDocumentMetadataChanges:includeMetadataChanges + waitForSyncWhenOnline:NO]; } @end diff --git a/Firestore/Source/API/FIRQuerySnapshot.mm b/Firestore/Source/API/FIRQuerySnapshot.mm index abee84c..fb7a849 100644 --- a/Firestore/Source/API/FIRQuerySnapshot.mm +++ b/Firestore/Source/API/FIRQuerySnapshot.mm @@ -62,6 +62,7 @@ NS_ASSUME_NONNULL_BEGIN // Cached value of the documentChanges property. NSArray<FIRDocumentChange *> *_documentChanges; + BOOL _documentChangesIncludeMetadataChanges; } - (instancetype)initWithFirestore:(FIRFirestore *)firestore @@ -73,6 +74,7 @@ NS_ASSUME_NONNULL_BEGIN _originalQuery = query; _snapshot = snapshot; _metadata = metadata; + _documentChangesIncludeMetadataChanges = NO; } return self; } @@ -139,9 +141,16 @@ NS_ASSUME_NONNULL_BEGIN } - (NSArray<FIRDocumentChange *> *)documentChanges { - if (!_documentChanges) { - _documentChanges = - [FIRDocumentChange documentChangesForSnapshot:self.snapshot firestore:self.firestore]; + return [self documentChangesWithIncludeMetadataChanges:NO]; +} + +- (NSArray<FIRDocumentChange *> *)documentChangesWithIncludeMetadataChanges: + (BOOL)includeMetadataChanges { + if (!_documentChanges || _documentChangesIncludeMetadataChanges != includeMetadataChanges) { + _documentChanges = [FIRDocumentChange documentChangesForSnapshot:self.snapshot + includeMetadataChanges:includeMetadataChanges + firestore:self.firestore]; + _documentChangesIncludeMetadataChanges = includeMetadataChanges; } return _documentChanges; } diff --git a/Firestore/Source/Public/FIRQuery.h b/Firestore/Source/Public/FIRQuery.h index ff15ac6..a28af39 100644 --- a/Firestore/Source/Public/FIRQuery.h +++ b/Firestore/Source/Public/FIRQuery.h @@ -25,46 +25,6 @@ NS_ASSUME_NONNULL_BEGIN -/** - * Options for use with `[FIRQuery addSnapshotListener]` to control the behavior of the snapshot - * listener. - */ -NS_SWIFT_NAME(QueryListenOptions) -@interface FIRQueryListenOptions : NSObject - -+ (instancetype)options NS_SWIFT_UNAVAILABLE("Use initializer"); - -- (instancetype)init; - -@property(nonatomic, assign, readonly) BOOL includeQueryMetadataChanges; - -/** - * Sets the includeQueryMetadataChanges option which controls whether metadata-only changes on the - * query (i.e. only `FIRQuerySnapshot.metadata` changed) should trigger snapshot events. Default is - * NO. - * - * @param includeQueryMetadataChanges Whether to raise events for metadata-only changes on the - * query. - * @return The receiver is returned for optional method chaining. - */ -- (instancetype)includeQueryMetadataChanges:(BOOL)includeQueryMetadataChanges - NS_SWIFT_NAME(includeQueryMetadataChanges(_:)); - -@property(nonatomic, assign, readonly) BOOL includeDocumentMetadataChanges; - -/** - * Sets the includeDocumentMetadataChanges option which controls whether document metadata-only - * changes (i.e. only `FIRDocumentSnapshot.metadata` on a document contained in the query - * changed) should trigger snapshot events. Default is NO. - * - * @param includeDocumentMetadataChanges Whether to raise events for document metadata-only changes. - * @return The receiver is returned for optional method chaining. - */ -- (instancetype)includeDocumentMetadataChanges:(BOOL)includeDocumentMetadataChanges - NS_SWIFT_NAME(includeDocumentMetadataChanges(_:)); - -@end - typedef void (^FIRQuerySnapshotBlock)(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error); @@ -103,16 +63,17 @@ NS_SWIFT_NAME(Query) /** * Attaches a listener for QuerySnapshot events. * - * @param options Options controlling the listener behavior. + * @param includeMetadataChanges Whether metadata-only changes (i.e. only + * `FIRDocumentSnapshot.metadata` changed) should trigger snapshot events. * @param listener The listener to attach. * * @return A FIRListenerRegistration that can be used to remove this listener. */ // clang-format off -- (id<FIRListenerRegistration>)addSnapshotListenerWithOptions: - (nullable FIRQueryListenOptions *)options - listener:(FIRQuerySnapshotBlock)listener - NS_SWIFT_NAME(addSnapshotListener(options:listener:)); +- (id<FIRListenerRegistration>) +addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges + listener:(FIRQuerySnapshotBlock)listener + NS_SWIFT_NAME(addSnapshotListener(includeMetadataChanges:listener:)); // clang-format on #pragma mark - Filtering Data diff --git a/Firestore/Source/Public/FIRQuerySnapshot.h b/Firestore/Source/Public/FIRQuerySnapshot.h index 1266832..6a7e60d 100644 --- a/Firestore/Source/Public/FIRQuerySnapshot.h +++ b/Firestore/Source/Public/FIRQuerySnapshot.h @@ -58,6 +58,16 @@ NS_SWIFT_NAME(QuerySnapshot) */ @property(nonatomic, strong, readonly) NSArray<FIRDocumentChange *> *documentChanges; +/** + * Returns an array of the documents that changed since the last snapshot. If this is the first + * snapshot, all documents will be in the list as Added changes. + * + * @param includeMetadataChanges Whether metadata-only changes (i.e. only + * `FIRDocumentSnapshot.metadata` changed) should be included. + */ +- (NSArray<FIRDocumentChange *> *)documentChangesWithIncludeMetadataChanges: + (BOOL)includeMetadataChanges NS_SWIFT_NAME(documentChanges(includeMetadataChanges:)); + @end NS_ASSUME_NONNULL_END |