Android's Firestore's snapshot listener on query takes too long (1 min) to update from the network when returning from being in the background

Viewed 546

I'm seeing terrible update frequency when resuming the app after it had been in the background for a while. If I kill and re-launch the app from scratch it'll usually query the most recent data fine. Similarly, when I first start listening for snapshots it works as expected, receiving data as soon as the firestore data is updated.

It is only after the app has been in the background for too long (say 1-2 hours) and I reopen it that I see the query return cached data but not receive an update from the network for 30-90 seconds.

Here's my query:

firestore.collection(GAMES_COLLECTION)
          .whereGreaterThanOrEqualTo("time_utc", start)
          .whereLessThanOrEqualTo("time_utc", end)
          .orderBy("time_utc")
          .addSnapshotListener { snapshot, e ->
            val games = snapshot.map { doc -> doc.toObject(GameEntity::class.java) }
          }

The "Game" document is not too complex either (19 primitive fields and 1 array field that has 0-3 elements)

The "time_utc" field is an int that has one of the automatic indexes enabled (ascending/descending)

I've tried subscribing in a couple of different ways to see if that's the problem but I really doubt that's the problem given that it's not a problem of the subscription being lost or something, if I stay for long enough (1 minute) I'll eventually get an update. I'm subscribing on jetpack's ViewModel's init and unsubscribing in onCleared() (i.e. fragment's onCreate/ onDestroy).

I also tried having a single "global" subscription (started in the Application class' onCreate()) in case it was a "cold start" issue but that doesn't work either.

Also tried using .get().addOnSuccessListener() on that same query through a manual refresh when I know the data is stale and I'm not getting network updates but it immediately returns the same cached data.

Offline persistence is enabled, I have not setup any caching limits.

Using firestore 21.5.0

EDIT: Found more folks dealing with the same exact problem but in the flutter SDK: https://github.com/FirebaseExtended/flutterfire/issues/4305
My Flutter app with Firestore experiences very slow queries when it is resumed from the background on Android

1 Answers

I don't know how firestore work internally, what happen with the connection in few hours in background, maybe there are connectivity manager or other constructs and the connection may terminated, but I think that for sure you shouldn't keep snapshotListener in the background for hours/days (when you click home button viewmodel's onClear() not called).

I think it's wrong way to add snapshotListener in viewmodel's init block and unsubscribe in onClear, you should subscribe/unsubscribe at least at onStart()/onStop().

This way you subscribing again in onStart() and the initial problem with delay disappear, and also you don't leak resources(connection) when the app in background.

Related