-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add GetOptions for controlling offline get behaviour #655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
80a40bd
6d29de2
30a513f
c2395cc
76d0ae8
2a32091
ec55882
61876ad
f5024fb
3abd449
d7b9e60
893e56a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ - (void)testGetDocumentWhileOnlineWithDefaultGetOptions { | |
FIRDocumentSnapshot *result = [self readDocumentForRef:doc]; | ||
XCTAssertTrue(result.exists); | ||
XCTAssertFalse(result.metadata.fromCache); | ||
XCTAssertFalse(result.metadata.hasPendingWrites); | ||
XCTAssertEqualObjects(result.data, initialData); | ||
} | ||
|
||
|
@@ -57,9 +58,15 @@ - (void)testGetCollectionWhileOnlineWithDefaultGetOptions { | |
// initialDocs. | ||
FIRQuerySnapshot *result = [self readDocumentSetForRef:col]; | ||
XCTAssertFalse(result.metadata.fromCache); | ||
XCTAssertFalse(result.metadata.hasPendingWrites); | ||
XCTAssertEqualObjects( | ||
FIRQuerySnapshotGetData(result), | ||
(@[ @{@"key1" : @"value1"}, @{@"key2" : @"value2"}, @{@"key3" : @"value3"} ])); | ||
XCTAssertEqualObjects(FIRQuerySnapshotGetDocChangesData(result), (@[ | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3" : @"value3"} ] | ||
])); | ||
} | ||
|
||
- (void)testGetDocumentWhileOfflineWithDefaultGetOptions { | ||
|
@@ -86,6 +93,7 @@ - (void)testGetDocumentWhileOfflineWithDefaultGetOptions { | |
FIRDocumentSnapshot *result = [self readDocumentForRef:doc]; | ||
XCTAssertTrue(result.exists); | ||
XCTAssertTrue(result.metadata.fromCache); | ||
XCTAssertTrue(result.metadata.hasPendingWrites); | ||
XCTAssertEqualObjects(result.data, newData); | ||
} | ||
|
||
|
@@ -113,10 +121,18 @@ - (void)testGetCollectionWhileOfflineWithDefaultGetOptions { | |
// get docs and ensure they *are* from the cache, and matches the updated data. | ||
FIRQuerySnapshot *result = [self readDocumentSetForRef:col]; | ||
XCTAssertTrue(result.metadata.fromCache); | ||
XCTAssertTrue(result.metadata.hasPendingWrites); | ||
XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[ | ||
@{@"key1" : @"value1"}, @{@"key2" : @"value2", @"key2b" : @"value2b"}, | ||
@{@"key3b" : @"value3b"}, @{@"key4" : @"value4"} | ||
])); | ||
XCTAssertEqualObjects( | ||
FIRQuerySnapshotGetDocChangesData(result), (@[ | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2", @"key2b" : @"value2b"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3b" : @"value3b"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc4", @{@"key4" : @"value4"} ] | ||
])); | ||
} | ||
|
||
- (void)testGetDocumentWhileOnlineCacheOnly { | ||
|
@@ -132,6 +148,7 @@ - (void)testGetDocumentWhileOnlineCacheOnly { | |
[self readDocumentForRef:doc options:[[FIRGetOptions alloc] initWithSource:FIRSourceCache]]; | ||
XCTAssertTrue(result.exists); | ||
XCTAssertTrue(result.metadata.fromCache); | ||
XCTAssertFalse(result.metadata.hasPendingWrites); | ||
XCTAssertEqualObjects(result.data, initialData); | ||
} | ||
|
||
|
@@ -152,11 +169,17 @@ - (void)testGetCollectionWhileOnlineCacheOnly { | |
[self readDocumentSetForRef:col | ||
options:[[FIRGetOptions alloc] initWithSource:FIRSourceCache]]; | ||
XCTAssertTrue(result.metadata.fromCache); | ||
XCTAssertFalse(result.metadata.hasPendingWrites); | ||
XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[ | ||
@{@"key1" : @"value1"}, | ||
@{@"key2" : @"value2"}, | ||
@{@"key3" : @"value3"}, | ||
])); | ||
XCTAssertEqualObjects(FIRQuerySnapshotGetDocChangesData(result), (@[ | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3" : @"value3"} ] | ||
])); | ||
} | ||
|
||
- (void)testGetDocumentWhileOfflineCacheOnly { | ||
|
@@ -184,6 +207,7 @@ - (void)testGetDocumentWhileOfflineCacheOnly { | |
[self readDocumentForRef:doc options:[[FIRGetOptions alloc] initWithSource:FIRSourceCache]]; | ||
XCTAssertTrue(result.exists); | ||
XCTAssertTrue(result.metadata.fromCache); | ||
XCTAssertTrue(result.metadata.hasPendingWrites); | ||
XCTAssertEqualObjects(result.data, newData); | ||
} | ||
|
||
|
@@ -214,10 +238,18 @@ - (void)testGetCollectionWhileOfflineCacheOnly { | |
[self readDocumentSetForRef:col | ||
options:[[FIRGetOptions alloc] initWithSource:FIRSourceCache]]; | ||
XCTAssertTrue(result.metadata.fromCache); | ||
XCTAssertTrue(result.metadata.hasPendingWrites); | ||
XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[ | ||
@{@"key1" : @"value1"}, @{@"key2" : @"value2", @"key2b" : @"value2b"}, | ||
@{@"key3b" : @"value3b"}, @{@"key4" : @"value4"} | ||
])); | ||
XCTAssertEqualObjects( | ||
FIRQuerySnapshotGetDocChangesData(result), (@[ | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2", @"key2b" : @"value2b"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3b" : @"value3b"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc4", @{@"key4" : @"value4"} ] | ||
])); | ||
} | ||
|
||
- (void)testGetDocumentWhileOnlineServerOnly { | ||
|
@@ -233,6 +265,7 @@ - (void)testGetDocumentWhileOnlineServerOnly { | |
[self readDocumentForRef:doc options:[[FIRGetOptions alloc] initWithSource:FIRSourceServer]]; | ||
XCTAssertTrue(result.exists); | ||
XCTAssertFalse(result.metadata.fromCache); | ||
XCTAssertFalse(result.metadata.hasPendingWrites); | ||
XCTAssertEqualObjects(result.data, initialData); | ||
} | ||
|
||
|
@@ -253,11 +286,17 @@ - (void)testGetCollectionWhileOnlineServerOnly { | |
[self readDocumentSetForRef:col | ||
options:[[FIRGetOptions alloc] initWithSource:FIRSourceServer]]; | ||
XCTAssertFalse(result.metadata.fromCache); | ||
XCTAssertFalse(result.metadata.hasPendingWrites); | ||
XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[ | ||
@{@"key1" : @"value1"}, | ||
@{@"key2" : @"value2"}, | ||
@{@"key3" : @"value3"}, | ||
])); | ||
XCTAssertEqualObjects(FIRQuerySnapshotGetDocChangesData(result), (@[ | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3" : @"value3"} ] | ||
])); | ||
} | ||
|
||
- (void)testGetDocumentWhileOfflineServerOnly { | ||
|
@@ -341,13 +380,15 @@ - (void)testGetDocumentWhileOfflineWithDifferentGetOptions { | |
[self readDocumentForRef:doc options:[[FIRGetOptions alloc] initWithSource:FIRSourceCache]]; | ||
XCTAssertTrue(result.exists); | ||
XCTAssertTrue(result.metadata.fromCache); | ||
XCTAssertTrue(result.metadata.hasPendingWrites); | ||
XCTAssertEqualObjects(result.data, newData); | ||
|
||
// attempt to get doc (with default get options) | ||
result = | ||
[self readDocumentForRef:doc options:[[FIRGetOptions alloc] initWithSource:FIRSourceDefault]]; | ||
XCTAssertTrue(result.exists); | ||
XCTAssertTrue(result.metadata.fromCache); | ||
XCTAssertTrue(result.metadata.hasPendingWrites); | ||
XCTAssertEqualObjects(result.data, newData); | ||
|
||
// attempt to get doc (from the server) and ensure it cannot be retreived | ||
|
@@ -398,10 +439,18 @@ - (void)testGetCollectionWhileOfflineWithDifferentGetOptions { | |
[self readDocumentSetForRef:col | ||
options:[[FIRGetOptions alloc] initWithSource:FIRSourceCache]]; | ||
XCTAssertTrue(result.metadata.fromCache); | ||
XCTAssertTrue(result.metadata.hasPendingWrites); | ||
XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[ | ||
@{@"key1" : @"value1"}, @{@"key2" : @"value2", @"key2b" : @"value2b"}, | ||
@{@"key3b" : @"value3b"}, @{@"key4" : @"value4"} | ||
])); | ||
XCTAssertEqualObjects( | ||
FIRQuerySnapshotGetDocChangesData(result), (@[ | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2", @"key2b" : @"value2b"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3b" : @"value3b"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc4", @{@"key4" : @"value4"} ] | ||
])); | ||
|
||
// attempt to get docs (with default get options) | ||
result = [self readDocumentSetForRef:col | ||
|
@@ -411,6 +460,13 @@ - (void)testGetCollectionWhileOfflineWithDifferentGetOptions { | |
@{@"key1" : @"value1"}, @{@"key2" : @"value2", @"key2b" : @"value2b"}, | ||
@{@"key3b" : @"value3b"}, @{@"key4" : @"value4"} | ||
])); | ||
XCTAssertEqualObjects( | ||
FIRQuerySnapshotGetDocChangesData(result), (@[ | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc1", @{@"key1" : @"value1"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc2", @{@"key2" : @"value2", @"key2b" : @"value2b"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc3", @{@"key3b" : @"value3b"} ], | ||
@[ @(FIRDocumentChangeTypeAdded), @"doc4", @{@"key4" : @"value4"} ] | ||
])); | ||
|
||
// attempt to get docs (from the server) and ensure they cannot be retreived | ||
XCTestExpectation *failedGetDocsCompletion = [self expectationWithDescription:@"failedGetDocs"]; | ||
|
@@ -431,6 +487,7 @@ - (void)testGetNonExistingDocWhileOnlineWithDefaultGetOptions { | |
FIRDocumentSnapshot *snapshot = [self readDocumentForRef:doc]; | ||
XCTAssertFalse(snapshot.exists); | ||
XCTAssertFalse(snapshot.metadata.fromCache); | ||
XCTAssertFalse(snapshot.metadata.hasPendingWrites); | ||
} | ||
|
||
- (void)testGetNonExistingCollectionWhileOnlineWithDefaultGetOptions { | ||
|
@@ -439,7 +496,9 @@ - (void)testGetNonExistingCollectionWhileOnlineWithDefaultGetOptions { | |
// get collection and ensure it's empty and that it's *not* from the cache. | ||
FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:col]; | ||
XCTAssertEqual(snapshot.count, 0); | ||
XCTAssertEqual(snapshot.documentChanges.count, 0); | ||
XCTAssertFalse(snapshot.metadata.fromCache); | ||
XCTAssertFalse(snapshot.metadata.hasPendingWrites); | ||
} | ||
|
||
- (void)testGetNonExistingDocWhileOfflineWithDefaultGetOptions { | ||
|
@@ -471,7 +530,9 @@ - (void)testGetNonExistingCollectionWhileOfflineWithDefaultGetOptions { | |
// get collection and ensure it's empty and that it *is* from the cache. | ||
FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:col]; | ||
XCTAssertEqual(snapshot.count, 0); | ||
XCTAssertEqual(snapshot.documentChanges.count, 0); | ||
XCTAssertTrue(snapshot.metadata.fromCache); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also check empty. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Done.) |
||
XCTAssertFalse(snapshot.metadata.hasPendingWrites); | ||
} | ||
|
||
- (void)testGetNonExistingDocWhileOnlineCacheOnly { | ||
|
@@ -500,7 +561,9 @@ - (void)testGetNonExistingCollectionWhileOnlineCacheOnly { | |
[self readDocumentSetForRef:col | ||
options:[[FIRGetOptions alloc] initWithSource:FIRSourceCache]]; | ||
XCTAssertEqual(snapshot.count, 0); | ||
XCTAssertEqual(snapshot.documentChanges.count, 0); | ||
XCTAssertTrue(snapshot.metadata.fromCache); | ||
XCTAssertFalse(snapshot.metadata.hasPendingWrites); | ||
} | ||
|
||
- (void)testGetNonExistingDocWhileOfflineCacheOnly { | ||
|
@@ -535,7 +598,9 @@ - (void)testGetNonExistingCollectionWhileOfflineCacheOnly { | |
[self readDocumentSetForRef:col | ||
options:[[FIRGetOptions alloc] initWithSource:FIRSourceCache]]; | ||
XCTAssertEqual(snapshot.count, 0); | ||
XCTAssertEqual(snapshot.documentChanges.count, 0); | ||
XCTAssertTrue(snapshot.metadata.fromCache); | ||
XCTAssertFalse(snapshot.metadata.hasPendingWrites); | ||
} | ||
|
||
- (void)testGetNonExistingDocWhileOnlineServerOnly { | ||
|
@@ -546,6 +611,7 @@ - (void)testGetNonExistingDocWhileOnlineServerOnly { | |
[self readDocumentForRef:doc options:[[FIRGetOptions alloc] initWithSource:FIRSourceServer]]; | ||
XCTAssertFalse(snapshot.exists); | ||
XCTAssertFalse(snapshot.metadata.fromCache); | ||
XCTAssertFalse(snapshot.metadata.hasPendingWrites); | ||
} | ||
|
||
- (void)testGetNonExistingCollectionWhileOnlineServerOnly { | ||
|
@@ -556,7 +622,9 @@ - (void)testGetNonExistingCollectionWhileOnlineServerOnly { | |
[self readDocumentSetForRef:col | ||
options:[[FIRGetOptions alloc] initWithSource:FIRSourceServer]]; | ||
XCTAssertEqual(snapshot.count, 0); | ||
XCTAssertEqual(snapshot.documentChanges.count, 0); | ||
XCTAssertFalse(snapshot.metadata.fromCache); | ||
XCTAssertFalse(snapshot.metadata.hasPendingWrites); | ||
} | ||
|
||
- (void)testGetNonExistingDocWhileOfflineServerOnly { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -325,6 +325,18 @@ - (void)waitUntil:(BOOL (^)())predicate { | |
return result; | ||
} | ||
|
||
extern "C" NSArray<NSArray<id> *> *FIRQuerySnapshotGetDocChangesData(FIRQuerySnapshot *docs) { | ||
NSMutableArray<NSMutableArray<id> *> *result = [NSMutableArray array]; | ||
for (FIRDocumentChange *docChange in docs.documentChanges) { | ||
NSMutableArray<id> *docChangeData = [NSMutableArray array]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two bits of feedback here:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
[docChangeData addObject:@(docChange.type)]; | ||
[docChangeData addObject:docChange.document.documentID]; | ||
[docChangeData addObject:docChange.document.data]; | ||
[result addObject:docChangeData]; | ||
} | ||
return result; | ||
} | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
#import "Firestore/Source/Core/FSTQuery.h" | ||
#import "Firestore/Source/Core/FSTSyncEngine.h" | ||
#import "Firestore/Source/Core/FSTTransaction.h" | ||
#import "Firestore/Source/Core/FSTView.h" | ||
#import "Firestore/Source/Local/FSTEagerGarbageCollector.h" | ||
#import "Firestore/Source/Local/FSTLevelDB.h" | ||
#import "Firestore/Source/Local/FSTLocalSerializer.h" | ||
|
@@ -287,27 +288,23 @@ - (void)getDocumentsFromLocalCache:(FIRQuery *)query | |
completion:(void (^)(FIRQuerySnapshot *_Nullable query, | ||
NSError *_Nullable error))completion { | ||
[self.workerDispatchQueue dispatchAsync:^{ | ||
|
||
FSTDocumentDictionary *docs = [self.localStore executeQuery:query.query]; | ||
FSTDocumentKeySet *remoteKeys = [FSTDocumentKeySet keySet]; | ||
|
||
__block FSTDocumentSet *documents = | ||
[FSTDocumentSet documentSetWithComparator:query.query.comparator]; | ||
FSTDocumentSet *oldDocuments = documents; | ||
[docs enumerateKeysAndObjectsUsingBlock:^(FSTDocumentKey *key, FSTDocument *value, BOOL *stop) { | ||
documents = [documents documentSetByAddingDocument:value]; | ||
}]; | ||
FSTView *view = [[FSTView alloc] initWithQuery:query.query remoteDocuments:remoteKeys]; | ||
FSTViewDocumentChanges *viewDocChanges = [view computeChangesWithDocuments:docs]; | ||
FSTViewChange *viewChange = [view applyChangesToDocuments:viewDocChanges]; | ||
FSTAssert(viewChange.limboChanges.count == 0, | ||
@"View returned limbo docs before target ack from the server."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assertion text makes no sense in this context (there won't be a target ack from the server since we're not starting a listen with the server). Rather this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
FSTViewSnapshot *snapshot = [[FSTViewSnapshot alloc] initWithQuery:query.query | ||
documents:documents | ||
oldDocuments:oldDocuments | ||
documentChanges:@[] | ||
fromCache:YES | ||
hasPendingWrites:NO | ||
syncStateChanged:NO]; | ||
FSTViewSnapshot *snapshot = viewChange.snapshot; | ||
FIRSnapshotMetadata *metadata = | ||
[FIRSnapshotMetadata snapshotMetadataWithPendingWrites:NO fromCache:YES]; | ||
[FIRSnapshotMetadata snapshotMetadataWithPendingWrites:snapshot.hasPendingWrites | ||
fromCache:snapshot.fromCache]; | ||
|
||
completion([FIRQuerySnapshot snapshotWithFirestore:query.firestore | ||
originalQuery:query.query | ||
originalQuery:query | ||
snapshot:snapshot | ||
metadata:metadata], | ||
nil); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should also be empty, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done (throughout)