5

I am using firestore onSnapshot methods in a React Native app on several different sets of data in my app, and for the most part they all appear to work fine. However, after a period of time, perhaps after the app is put into background and re-awakened, the onSnapshot methods stop updating the data.

It is as if they have switched off. Sometimes they seem to stop working just after a period of time, without the app being backgrounded.

If I call the methods again then they start working again, but that seems to defeat the object of using them, I may as well just use normal get() methods and call them when I need.

Surely someone else has experienced this and can help?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Are you using the `react-native-firebase` library, or just `firebase`? – romin21 Apr 08 '19 at 14:25
  • 1
    I am using https://github.com/invertase/react-native-firebase As I say, for the most part it all works as expected, but sometimes it just doesn't, with no explanation. For example, I am outputting a list of objects called "groups" and there is an onSnapshot on that set of data. Sometimes when I add a group, it appears instantly in the app, exactly as it should, and sometimes it does not - until I do a manually pull of the data, when it does appear – Matt Ormrod Apr 08 '19 at 14:42
  • 1
    Observing same issue. Also mentioned in https://github.com/invertase/react-native-firebase/issues/1992 – udit bansal Apr 10 '19 at 16:42

2 Answers2

2

I had the same problem using onsnapshot in a script running in node as firebase client. I fixed it by recalling onsnapshot both after some time interval or an error. In my case after 30 minutes.

Like this (firebase 9):

let onSnapRemove;
function onSnap() {
    onSnapRemove = onSnapshot(coll, {
        next: snap => {
            ...
        },
        error: err => {
            console.log(err);
            onSnapRemove();
            onSnap();
        }
    });
}

onSnap();

function repeat() {
    if (typeof onSnapRemove == 'function') onSnapRemove();
    onSnap();
}
setInterval(() => {
    repeat();
}, 1000 * 60 * 30);
  • 1
    I have noticed the same problem in my Node.js application using [`firebase-admin`](https://github.com/firebase/firebase-admin-node). It stops working after roughly 24 hours for multiple customers in production. My solution was to add [`cron_restart`](https://pm2.keymetrics.io/docs/usage/restart-strategies/) to my `pm2.json` that runs the app, but this solution is better. – xinthose May 19 '23 at 13:44
0

When an error occurs, Firebase will invoke the (optional) second callback that you can pass in to functions like onSnapshot, or a simple get. E.g.

const coleccionRef = database.ref('test');
coleccionRef.on('value', snapshot => {
    snapshot.val();
}, error => {
    console.error(error);
});

You can inspect that error to understand what is going wrong.

romin21
  • 1,532
  • 1
  • 14
  • 35
  • 2
    I think I've tried this before and there was no error logged, but I will try again and see if that shows any information – Matt Ormrod Apr 08 '19 at 14:51
  • Normally a Firebase real time listener (`onSnapshot`) or even `get` will either return what you need, or an error if something goes wrong, so I really think this is the right way to go. Let me know if this is not the case with your project. – romin21 Apr 08 '19 at 14:53