Skip to content

Commit 8948318

Browse files
authored
Add GetOptions for controlling offline get behaviour (firebase#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 28c27de commit 8948318

File tree

13 files changed

+949
-16
lines changed

13 files changed

+949
-16
lines changed

Firestore/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
`FIRServerTimestampBehavior` on `DocumentSnapshot`. Instead of calling
2525
`data(SnapshotOptions.serverTimestampBehavior(.estimate))` call
2626
`data(serverTimestampBehavior: .estimate)`. Changed `get` similarly.
27+
- [changed] Added ability to control whether DocumentReference.getDocument() and
28+
Query.getDocuments() should fetch from server only, cache only, or attempt
29+
server and fall back to the cache (which was the only option previously, and
30+
is now the default.)
2731

2832
# v0.11.0
2933
- [fixed] Fixed a regression in the Firebase iOS SDK release 4.11.0 that could

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
6003F5B1195388D20070C39A /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F58D195388D20070C39A /* Foundation.framework */; };
132132
6003F5B2195388D20070C39A /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6003F591195388D20070C39A /* UIKit.framework */; };
133133
6003F5BA195388D20070C39A /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6003F5B8195388D20070C39A /* InfoPlist.strings */; };
134+
6161B5032047140C00A99DBB /* FIRFirestoreSourceTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6161B5012047140400A99DBB /* FIRFirestoreSourceTests.mm */; };
134135
6ED54761B845349D43DB6B78 /* Pods_Firestore_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 75A6FE51C1A02DF38F62FAAD /* Pods_Firestore_Example.framework */; };
135136
71719F9F1E33DC2100824A3D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 71719F9D1E33DC2100824A3D /* LaunchScreen.storyboard */; };
136137
7346E61D20325C6900FD6CEF /* FSTDispatchQueueTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7346E61C20325C6900FD6CEF /* FSTDispatchQueueTests.mm */; };
@@ -356,6 +357,7 @@
356357
6003F5AF195388D20070C39A /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
357358
6003F5B7195388D20070C39A /* Tests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Tests-Info.plist"; sourceTree = "<group>"; };
358359
6003F5B9195388D20070C39A /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
360+
6161B5012047140400A99DBB /* FIRFirestoreSourceTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FIRFirestoreSourceTests.mm; sourceTree = "<group>"; };
359361
69F6A10DBD6187489481CD76 /* Pods_Firestore_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Firestore_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
360362
71719F9E1E33DC2100824A3D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
361363
7346E61C20325C6900FD6CEF /* FSTDispatchQueueTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = FSTDispatchQueueTests.mm; sourceTree = "<group>"; };
@@ -885,6 +887,7 @@
885887
isa = PBXGroup;
886888
children = (
887889
73866A9F2082B069009BB4FF /* FIRArrayTransformTests.mm */,
890+
6161B5012047140400A99DBB /* FIRFirestoreSourceTests.mm */,
888891
5492E070202154D600B64F25 /* FIRCursorTests.mm */,
889892
5492E06C202154D500B64F25 /* FIRDatabaseTests.mm */,
890893
5492E06A202154D500B64F25 /* FIRFieldsTests.mm */,
@@ -1559,6 +1562,7 @@
15591562
buildActionMask = 2147483647;
15601563
files = (
15611564
73866AA12082B0A5009BB4FF /* FIRArrayTransformTests.mm in Sources */,
1565+
6161B5032047140C00A99DBB /* FIRFirestoreSourceTests.mm in Sources */,
15621566
5492E076202154D600B64F25 /* FIRValidationTests.mm in Sources */,
15631567
5492E072202154D600B64F25 /* FIRQueryTests.mm in Sources */,
15641568
5491BC731FB44593008B3588 /* FSTIntegrationTestCase.mm in Sources */,

Firestore/Example/SwiftBuildTest/main.swift

Lines changed: 20 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+
readDocumentWithSource(at: documentRef)
3536

3637
readDocuments(matching: query)
38+
readDocumentsWithSource(matching: query)
3739

3840
listenToDocument(at: documentRef)
3941

@@ -230,6 +232,15 @@ func readDocument(at docRef: DocumentReference) {
230232
}
231233
}
232234

235+
func readDocumentWithSource(at docRef: DocumentReference) {
236+
docRef.getDocument(source: FirestoreSource.default) { document, error in
237+
}
238+
docRef.getDocument(source: .server) { document, error in
239+
}
240+
docRef.getDocument(source: FirestoreSource.cache) { document, error in
241+
}
242+
}
243+
233244
func readDocuments(matching query: Query) {
234245
query.getDocuments { querySnapshot, error in
235246
// TODO(mikelehen): Figure out how to make "for..in" syntax work
@@ -240,6 +251,15 @@ func readDocuments(matching query: Query) {
240251
}
241252
}
242253

254+
func readDocumentsWithSource(matching query: Query) {
255+
query.getDocuments(source: FirestoreSource.default) { querySnapshot, error in
256+
}
257+
query.getDocuments(source: .server) { querySnapshot, error in
258+
}
259+
query.getDocuments(source: FirestoreSource.cache) { querySnapshot, error in
260+
}
261+
}
262+
243263
func listenToDocument(at docRef: DocumentReference) {
244264
let listener = docRef.addSnapshotListener { document, error in
245265
if let error = error {

0 commit comments

Comments
 (0)