firestore_cache icon indicating copy to clipboard operation
firestore_cache copied to clipboard

Offline delay

Open Nico04 opened this issue 1 year ago • 10 comments

Plugin works great in normal conditions. But if, after cache is first initiated, device goes offline, the getDocuments methods takes a while before returning cached data. Looking at the code, it may come from the line await cacheDocRef.get(); in isFetchDocuments, which tries to get the date from the server first, even if offline. Then after a while (I guess the default Firestore timeout), it returns the cached data then return cached data. Is there any way to improve this ? If it's offline, no need to check date at all, don't you think ?

EDIT : Maybe just add a new parameter ignoreServer (or cacheOnly) to getDocuments that completly ignore server date check, returning cached data directly ? And application will then set that to true when it detected that device is offline.

EDIT2 : Another feature idea could be to add a new method getDocumentsSnapshots that returns a Stream just like the Firestore snapshots() method, but returning cached data immediatly, than checks server's data, download it if needed, and return fresh new data in the stream if needed. That way, UI will display cached data immediatly, then updates if new version is available (if online).

Nico04 avatar Aug 18 '23 06:08 Nico04

I've spent few hours on this, and created a fork with my work : https://github.com/Nico04/firestore_cache If you are interested in a collaboration, tell me, I'll prepare a PR.

Nico04 avatar Aug 18 '23 15:08 Nico04

Hey @Nico04, thanks for raising the issue and the potential fix for it. I was thinking an alternative solution here where the plugin would check whether the device has network connection, if it doesn't, it would just fetch the data from the cache.

This would minimise the impact of existing users for the plugin, and they can benefit from the change without adopting to any breaking changes. What do you think about this?

zeshuaro avatar Aug 21 '23 10:08 zeshuaro

I've tried this solution using connectivity_plus package. While delay is much much lower than just waiting the timeout, there still is some delay, caused by the plugin. So if you call getDocuments just once it's fine, and, as you said, a quite simple and good idea. But if you call getDocuments several time (for multiple collections), delays will sum-up for no good reason. That's why, in my implementation, I've decided NOT to use this package inside getDocuments directly, instead leaving the caller to decide.

Nico04 avatar Aug 21 '23 12:08 Nico04

That's why, in my implementation, I've decided NOT to use this package inside getDocuments directly, instead leaving the caller to decide.

But wouldn't the caller have to do the same thing anyway to check whether the device is online/offline so that it can pass the appropriate argument into the function?

zeshuaro avatar Aug 28 '23 11:08 zeshuaro

That's why, in my implementation, I've decided NOT to use this package inside getDocuments directly, instead leaving the caller to decide.

But wouldn't the caller have to do the same thing anyway to check whether the device is online/offline so that it can pass the appropriate argument into the function?

He might, but not necessarily. As I said, you might want to make only one check for several call to getDocuments, or you might want to use another package, or another method.

Nico04 avatar Aug 28 '23 12:08 Nico04

I see, that makes sense. I think having the option to fetch from cache first then server would be beneficial, so feel free to raise a PR with those changes.

But for the new method getDocumentsSnapshots, I think that requires a bit more discussions so if you want to work on that change then maybe raise another PR separately, and then we can discuss there instead.

zeshuaro avatar Aug 28 '23 12:08 zeshuaro

I've spent quite some time on this project already. While first commits of my fork where quite close, now they includes all changes I need for my project. So I won't make a PR for just this feature soon. Feel free to use my fork, I'll be happy it'll help.

Nico04 avatar Sep 11 '23 06:09 Nico04

I think this package doesn't work in flutter web.

donuthappy avatar Dec 04 '23 16:12 donuthappy

@donuthappy is your issue related to any of the discussion here? If not, would you be able to raise a new issue, and share any errors/logs that would help me to reproduce/debug the issue?

zeshuaro avatar Dec 05 '23 07:12 zeshuaro

Hi @zeshuaro Sorry for that. I have one question. Firebase cache doesn't work in flutter web?

donuthappy avatar Dec 05 '23 07:12 donuthappy