firebase-kotlin-sdk icon indicating copy to clipboard operation
firebase-kotlin-sdk copied to clipboard

Add DocumentReference.get(source: Source = Source.DEFAULT) to firebase-firestore for iOS/Android/JS

Open fmasa opened this issue 3 years ago • 2 comments

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 of DocumentReference.get()

Underlying API


I can help with implementation but I don't have experience with iOS pipeline (nor I have iOS device to test the implementation)

fmasa avatar Dec 18 '21 11:12 fmasa

I don't know how I could help but this would be indeed useful :)

izadiegizabal avatar Oct 20 '22 14:10 izadiegizabal

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)
})

Qw4z1 avatar Nov 02 '23 04:11 Qw4z1

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?

Qw4z1 avatar Apr 09 '24 14:04 Qw4z1

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 avatar Apr 10 '24 07:04 Daeda88

@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. =)

Qw4z1 avatar Apr 11 '24 10:04 Qw4z1

Good. I have no merging rights though, so my approval is slightly worthless 😂

Daeda88 avatar Apr 11 '24 13:04 Daeda88

Just needs the tests and then we can merge it

nbransby avatar Apr 13 '24 05:04 nbransby