Skip to content

[Firestore] getDocumentsWithCompletion does not pull from cache when app is in offline mode. #485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lukegeiger opened this issue Nov 22, 2017 · 15 comments

Comments

@lukegeiger
Copy link

  • Xcode version: any
  • Firebase SDK version: n/a
  • Firebase Component: firestore
  • Component version:any

[REQUIRED] Step 3: Describe the problem

getDocumentsWithCompletion does not pull from cache when app is in offline mode.

Steps to reproduce:

  1. Query a list of data offline (or online) (ex: fetch a list of teams)
  2. call updateData on any object from that list you fetched offline. (ex: change the name of a team)
  3. Again query data while offline. The fetched data will not be updated with any change stated in point

I have been able to sort of work around this by instead of calling getDocumentsWithCompletion I am calling addSnapshotListenerWithOptions on a query and I am then getting a updated value when I re query again offline, however if I relaunch the app any updates I made to any object are again gone.

I really don't want to use any snapshots in my application, I never have a time where I need a constant connection on something and I never want to manage the listeners on a snapshot. My desired use case is to just pull from my database once, either from web while having an internet connection, or from the cache while offline, without having to juggle listeners. Is this possible?

In short, how does one update an object while in offline mode and see those changes persist?

@wilhuff
Copy link
Contributor

wilhuff commented Nov 22, 2017

Question: at step three you're getting a callback to getDocumentsWithCompletion but the snapshot you're getting doesn't include the write from step 2?

If that's the case this is a very serious bug :-(.

@lukegeiger
Copy link
Author

lukegeiger commented Nov 22, 2017 via email

@wilhuff
Copy link
Contributor

wilhuff commented Nov 22, 2017 via email

@lukegeiger
Copy link
Author

lukegeiger commented Nov 22, 2017

Hey will here are my logs from the following situation. I emailed them to you.

  1. Launch app in offline mode.
  2. Get organization objects from firestore
  3. update an "organization" name property
  4. Refetch organization objects

@lukegeiger
Copy link
Author

To add to this, the property change I made in point#3 isn't anywhere in the logs. However, this change does propagate to the model once I regain internet access, and the updates take affect so its not like its completely disappearing. The issue seems to be that the getDocuments request doesn't pull ever from an updated cache?

@lukegeiger
Copy link
Author

Want to bump this thread as I think there is a pretty big issue here and its currently severely impacting my apps performance. Please let me know If I can be of any further help.

@wilhuff
Copy link
Contributor

wilhuff commented Nov 30, 2017

I can't reproduce what you're describing.

I've written an additional test that specifically attempts to do what you've written up and the test is able to observe the new values:

Does the test here actually implement what you've described?
#513

So long as the document already exists, updates to the document will be visible offline.

If this test implements what you've described could you share a small program that reproduces what you're seeing?

@lukegeiger
Copy link
Author

@wilhuff Gotcha, I just emailed you a sample project.

@wilhuff
Copy link
Contributor

wilhuff commented Dec 1, 2017

So good news, bad news. The good news is that with the sample and the Network Link Conditioner I was able to reproduce the problem. Further good news is that you're experiencing a known issue that we're already working on fixing.

The bad news is that the fix is not easy and won't happen right away.

The Firestore SDK uses gRPC as its transport to the backend. When you force the connection to 100% packet loss gRPC is slow to fail the RPC stream. Firestore, in turn is conservative about transient failures and tries one more time to connect to the backend. The combination means that in this scenario it can take upward of 3 minutes for the Firestore client to determine that it's offline.

The specific problem you're encountering is that while the Firestore client thinks its online getDocumentsWithCompletion: will try to get fresh results from the server rather than just serving from the cache. Unlike the listen-based APIs, getDocumentsWithCompletion: can only return one snapshot and unfortunately because it takes so long to detect we're offline it waits the full 3 minutes before giving up and returning from cache.

If you wait that long you'll see the result you want.

The work-around for now is to use a listener. You can either start one in the background and just keep the latest snapshot around for your call to getNotesWithCompletion or start one in there and wait for a little bit to see if you can get fresh results from the server.

@lukegeiger
Copy link
Author

lukegeiger commented Dec 1, 2017

Gotcha thanks for digging. Sounds like a pretty serious issue. I've actually tried the listener approach, and although I can see my writes instantly after an update while offline, they wont persist after app launch. So for example If I do the listener approach, make an update while offline, kill app and come back the change wont be there anymore. I can prob swap around that demo project I made to replicate if you need me to reproduce.

@lukegeiger
Copy link
Author

hey @wilhuff any update on this one?

@wilhuff
Copy link
Contributor

wilhuff commented Jan 22, 2018

We're currently working on this fix (though the first implementation is in the javascript SDK).

@lukegeiger
Copy link
Author

Hey Gill thanks for the quick reply and awesome to hear it is being worked on. What about what I mentioned with the listener approach also not working quite right? Any ideas there?

@srolesen
Copy link

srolesen commented Feb 26, 2018

Thank you for finding this, have you found any workaround so that you can effectively update and then query the updated local database while offline ?

@wilhuff
Copy link
Contributor

wilhuff commented May 15, 2018

This was initially fixed in #872 (released in v0.10.4) and tuned in #978 (released in v0.11.0).

@wilhuff wilhuff closed this as completed May 15, 2018
@firebase firebase locked and limited conversation to collaborators Nov 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants