Skip to content

Commit 3440fef

Browse files
authored
Add GetOptions for controlling offline get behaviour (#655)
Add option to allow the user to control where DocumentReference.getDocument() and CollectionReference.getDocuments() fetches from. By default, it fetches from the server (if possible) and falls back to the local cache. It's now possible to alternatively fetch from the local cache only, or to fetch from the server only (though in the server only case, latency compensation is still enabled).
1 parent 4acdbdb commit 3440fef

File tree

14 files changed

+1031
-16
lines changed

14 files changed

+1031
-16
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
6003F5B1195388D20070C39A /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F58D195388D20070C39A /* Foundation.framework */; };
6060
6003F5B2195388D20070C39A /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F591195388D20070C39A /* UIKit.framework */; };
6161
6003F5BA195388D20070C39A /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6003F5B8195388D20070C39A /* InfoPlist.strings */; };
62+
61CC13FA2007D0C90021F5BF /* FIRGetOptionsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 61CC13F82007D0C20021F5BF /* FIRGetOptionsTests.m */; };
6263
61E1D8B11FCF6C5700753285 /* StringViewTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 61E1D8AF1FCF6AF500753285 /* StringViewTests.mm */; };
6364
6ED54761B845349D43DB6B78 /* Pods_Firestore_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 75A6FE51C1A02DF38F62FAAD /* Pods_Firestore_Example.framework */; };
6465
71719F9F1E33DC2100824A3D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 71719F9D1E33DC2100824A3D /* LaunchScreen.storyboard */; };
@@ -235,6 +236,7 @@
235236
6003F5AF195388D20070C39A /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
236237
6003F5B7195388D20070C39A /* Tests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Tests-Info.plist"; sourceTree = "<group>"; };
237238
6003F5B9195388D20070C39A /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
239+
61CC13F82007D0C20021F5BF /* FIRGetOptionsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRGetOptionsTests.m; sourceTree = "<group>"; };
238240
61E1D8AF1FCF6AF500753285 /* StringViewTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = StringViewTests.mm; sourceTree = "<group>"; };
239241
69F6A10DBD6187489481CD76 /* Pods_Firestore_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Firestore_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
240242
71719F9E1E33DC2100824A3D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
@@ -723,6 +725,7 @@
723725
DE51B1BC1F0D48AC0013853F /* API */ = {
724726
isa = PBXGroup;
725727
children = (
728+
61CC13F82007D0C20021F5BF /* FIRGetOptionsTests.m */,
726729
DE51B1BD1F0D48AC0013853F /* FIRCursorTests.m */,
727730
DE51B1BE1F0D48AC0013853F /* FIRDatabaseTests.m */,
728731
DE51B1BF1F0D48AC0013853F /* FIRFieldsTests.m */,
@@ -1281,6 +1284,7 @@
12811284
54E928251F33953400C1953E /* FSTEventAccumulator.m in Sources */,
12821285
DE03B2ED1F214BA200A30B9C /* FSTSmokeTests.m in Sources */,
12831286
DE03B2F31F214BAA00A30B9C /* FIRQueryTests.m in Sources */,
1287+
61CC13FA2007D0C90021F5BF /* FIRGetOptionsTests.m in Sources */,
12841288
DE03B35E1F21586C00A30B9C /* FSTHelpers.m in Sources */,
12851289
DE03B2F51F214BAA00A30B9C /* FIRTypeTests.m in Sources */,
12861290
DE03B2EF1F214BAA00A30B9C /* FIRCursorTests.m in Sources */,

Firestore/Example/SwiftBuildTest/main.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ func main() {
3232
addDocument(to: collectionRef);
3333

3434
readDocument(at: documentRef);
35+
readDocumentWithOptions(at: documentRef);
3536

3637
readDocuments(matching: query);
38+
readDocumentsWithOptions(matching: query);
3739

3840
listenToDocument(at: documentRef);
3941

@@ -223,6 +225,17 @@ func readDocument(at docRef: DocumentReference) {
223225
}
224226
}
225227

228+
func readDocumentWithOptions(at docRef: DocumentReference) {
229+
docRef.getDocument(options:GetOptions.defaultOptions()) { document, error in
230+
}
231+
docRef.getDocument(options:GetOptions(source:GetSource.default)) { document, error in
232+
}
233+
docRef.getDocument(options:GetOptions(source:.server)) { document, error in
234+
}
235+
docRef.getDocument(options:GetOptions(source:GetSource.cache)) { document, error in
236+
}
237+
}
238+
226239
func readDocuments(matching query: Query) {
227240
query.getDocuments() { querySnapshot, error in
228241
// TODO(mikelehen): Figure out how to make "for..in" syntax work
@@ -233,6 +246,17 @@ func readDocuments(matching query: Query) {
233246
}
234247
}
235248

249+
func readDocumentsWithOptions(matching query: Query) {
250+
query.getDocuments(options:GetOptions.defaultOptions()) { querySnapshot, error in
251+
}
252+
query.getDocuments(options:GetOptions.init(source:GetSource.default)) { querySnapshot, error in
253+
}
254+
query.getDocuments(options:GetOptions.init(source:GetSource.server)) { querySnapshot, error in
255+
}
256+
query.getDocuments(options:GetOptions.init(source:GetSource.cache)) { querySnapshot, error in
257+
}
258+
}
259+
236260
func listenToDocument(at docRef: DocumentReference) {
237261

238262
let listener = docRef.addSnapshotListener() { document, error in

0 commit comments

Comments
 (0)