You're right, of course. I'm sorry for assuming you guys had made such a strange design choice, and clearly I should have investigated a little further before mailing it in.
There actually are cases where a snapshot listener doesn't get an immediate cached result, even when the client should have one. Those are:
1. addSnapshotListener on a document reference for a document that doesn't exist, even if the client has previously tried to retrieve that document.
2. addSnapshotListener on a query that has no results, even if the client has previously issued that exact query.
I can understand it as a design decision -- it feels like there's a difference between getting a stale "nothing was there last time I checked" vs. a stale "here's what was there last time I checked" -- but it's a frustrating special case to deal with when the user has a bad connection.
For reference, here's what I'm doing, inside a ViewModel:
1. Watch all photoAccess documents with sharedWith == currentUserId, using query.addSnapshotListener.
2. For each document, get its "photoId" field and watch /photos/{photoId}, using documentReference.addSnapshotListener.
3. Publish the result to the ViewModel's MutableLiveData only once all snapshot listeners have returned at least once.
4. Until the result has been published, I display "loading." Once it has, I display the loaded photos.
So what goes wrong?
- If a user doesn't have any photoAccess objects, then rather than displaying "You have no photos" right away, the Activity will display "loading" until it hears from the server.
- If a photo has been deleted, but the corresponding photoAccess still exists, the Activity will display "loading" until it hears from the server. I try to prevent this from happening, of course.
- The logic of having to choose what to watch (step 2) based on what you've retrieved from a query (step 1) makes all this just a little bit trickier.
Again, this is in a situation where the client should know that there aren't any photoAccesses, or should know that there isn't a photo.
I intend to work around these issues by adding a .get(Source.Cached) before the addSnapshotListeners, though it isn't quite that simple, and it's frustrating to have to work around not getting offline cached results.
Cheers!
Bartholomew