Skip to content
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

enablePersistence doesn't work when the app starts offline #3905

Closed
moshfeu opened this issue Oct 7, 2020 · 4 comments
Closed

enablePersistence doesn't work when the app starts offline #3905

moshfeu opened this issue Oct 7, 2020 · 4 comments

Comments

@moshfeu
Copy link

moshfeu commented Oct 7, 2020

[REQUIRED] Describe your environment

  • Operating System version: macOS 10.15.6 (19G2021)
  • Browser version: Chrome Version 85.0.4183.121 (Official Build) (64-bit)
  • Firebase SDK version: 7.22.0
  • Firebase Product: realtime database, firestore

[REQUIRED] Describe the problem

Steps to reproduce:

  1. Initial regular firebase app.
  2. Add firebase.firestore().enablePersistence(); after the app initilization.
  3. Add app.database().goOffline(); (just to demonstrate offline mode. I also tested it without this line and without internet).
  4. Add db.ref().on( 'value', (snapshot) => { console.log(snapshot) })

The cosnole.log never fired.

If the app starts in online and then go offline, it works well.

A note: If I understand correctly, firebase.firestore().enablePersistence(); makes firebase storing a local version of the data but I couldn't find it anywhere (localStorage, cache, indexedb)

(Followed this doc: https://firebase.google.com/docs/firestore/manage-data/enable-offline#configure_offline_persistence)

Relevant Code:

https://codesandbox.io/s/charming-haslett-hfu7l?file=/src/App.js

const app = firebase.initializeApp(firebaseConfig);
app.firestore().enablePersistence();
app.database().goOffline();
app.database().ref().on(value, snapshot => console.log(snapshot))
@google-oss-bot
Copy link
Contributor

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@hsubox76
Copy link
Contributor

hsubox76 commented Oct 7, 2020

I might be misunderstanding, but are you enabling persistence in Firestore and then expecting it to enable persistence for Realtime Database? They are two separate products. If you want to use Firestore's offline persistence I believe you want to be querying Firestore (app.firestore().collection(items)) not Realtime Database (app.database().ref('items')). Realtime Database doesn't have an equivalent offline persistence feature (at least for Web).

Sorry if I'm misunderstanding what you're trying to do!

@moshfeu
Copy link
Author

moshfeu commented Oct 7, 2020

Apparently I was the one who misunderstanding. I didn't notice that this doc is under firesotre and that firestore is a totally different product. I thought that it another layer on top of the realtime db.
Sorry for that.

@moshfeu moshfeu closed this as completed Oct 7, 2020
@moshfeu
Copy link
Author

moshfeu commented Oct 8, 2020

If someone is interested, I implemented a partially offline support for firebase.
This service wraps firebase realtime api.

When the user online, each snapshot (on('value')) stored in localStorage.
When the client goes offline, the service, along with updating localStorage, it also add the changes in a queue. When the client is online again, the service send the updates to firebase and clean the queue.

As I said, it's only partially implemented but it can gives an idea and the basic for further work.

moshfeu/synced-shopping-list@7124fb8#diff-b13a37881b4b22091b672007c3d25647

@firebase firebase locked and limited conversation to collaborators Nov 7, 2020
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

3 participants