firebase-kotlin-sdk
firebase-kotlin-sdk copied to clipboard
Add DocumentReference.get(source: Source = Source.DEFAULT) to firebase-firestore for iOS/Android/JS
In all Firestore clients there is an option of passing source when trying to obtain current snapshot of document. This makes it possible to force reading from server.
Library | Class | Member | Platforms |
---|---|---|---|
firestore | DocumentReference | get(server: Server) | Android, iOS, JS |
This could be introduced in the same way it's available in Android client:
-
Source
enum - optional argument
source: Source = Source.DEFAULT
ofDocumentReference.get()
Underlying API
- Objective C client has method that takes
Source
enum - JS client has three separate methods:
-
getDocFromCache(reference) - behaves as
get(Source.CACHE)
- getDocFromServer(reference) - behaves as `get(Source.SERVER)
-
getDoc(reference) - behaves as
get(Source.DEFAULT)
-
getDocFromCache(reference) - behaves as
- Android client has method that takes
Source
enum
I can help with implementation but I don't have experience with iOS pipeline (nor I have iOS device to test the implementation)
I don't know how I could help but this would be indeed useful :)
It would be great to have this feature added. The ability to call documentReference.get(Source.CACHE)
is a life saver when it comes to keeping read counts down.
I might take a stab at this soon, but until then here is my expect/actual workaround:
// In data/firebase.kt
enum class Source {
CACHE,
SERVER,
DEFAULT
}
expect suspend fun DocumentReference.get(source: Source): DocumentSnapshot
// In data/firebase.android.kt
private fun data.Source.toAndroidSource() = when(this) {
data.Source.CACHE -> Source.CACHE
data.Source.SERVER -> Source.SERVER
data.Source.DEFAULT -> Source.DEFAULT
}
actual suspend fun DocumentReference.get(source: data.Source) =
DocumentSnapshot(android.get(source.toAndroidSource()).await())
// in data/firebase.ios.kt
private fun Source.toIosSource() = when(this) {
Source.CACHE -> FIRFirestoreSource.FIRFirestoreSourceCache
Source.SERVER -> FIRFirestoreSource.FIRFirestoreSourceServer
Source.DEFAULT -> FIRFirestoreSource.FIRFirestoreSourceDefault
}
actual suspend fun DocumentReference.get(source: Source) = DocumentSnapshot(awaitResult {
ios.getDocumentWithSource(source.toIosSource(), it)
})
Seems some of the native constructors are marked internal, so DocumentSnapshot(android.get(source.toAndroidSource()).await())
no longer works.
Some of the constructors are still available though, so if I use the Query object I'm able to do like below.
actual suspend fun Query.get(source: data.Source): QuerySnapshot =
QuerySnapshot(android.get(source.toAndroidSource()).await())
It seems to have changed in this commit. @Daeda88, was this intentional and is there another way to wrap native classes now?
We moved some things around and I guess it was just missed we need a constructor to do DocumentReference(android)
. The reason is this is now wrapped with an internal class and hence the default constructor had to be hidden as well. Feel free to add it back as a platform method.
E.g.
fun DocumentSnapshot(android: com.google.firebase.firestore.DocumentSnapshot): DocumentSnapshot = DocumentSnapshot(NativeDocumentSnapshot(android))
@Daeda88 Felt like #491 (which I saw you approved) was the right solution to my problem, so I'm going to leave this as is for now. =)
Good. I have no merging rights though, so my approval is slightly worthless 😂
Just needs the tests and then we can merge it