2

I have some RxFire code that listens to a Firestore collection query (representing channels) and, for each of the results, listens to a Realtime Database ref for documents (representing messages in that channel).

The problem I'm running into is that the Realtime Database documents are re-downloaded every time the Firestore query changes, even if they're for a path/reference that hasn't changed.

Here's some pseudo-code:

collection(channelsQuery).pipe(

  // Emits full array of channels whenever the query changes
  switchMap(channels => {

    return combineLatest(
      channels.map(channel =>

        // Emits the full set of messages for a given channel
        list(getMessagesRef(channel)),
      ),
    );
  })
)

Imagine the following scenario:

  • Query intially emits 3 Firestore channel documents
  • Observables are created for corresponding Realtime Database refs for those 3 channels, which emit their message documents
  • A new Firestore document is added that matches the original query, which now emits 4 channel documents
  • The previous observables for Realtime Database are destroyed, and new ones are created for the now 4 channels, re-downloading and emitting all the data it already had for the previous 3.

Obviously this is not ideal as it causes a lot of redundant reads on the Realtime Database. What's the best practice in this case? Keep in mind that when a channel is removed, I would like to destroy the corresponding observable, which switchMap already does.

0 Answers0