0

I am trying to listen to my document using the Snapshot Listener method in Android.

I have a document called 'Mobile' and a field called 'UUID'. In 'UUID', I keep all the UUID's of the phones the person has logged into.

The problem with this code is that I am not able to get listeners continuously with this code. Can we use Android Services or something to continuously check for changes in the document or a field in the document.

I need to create a code which will be continuously checking the UUID field for any changes and the code should create a Toast accordingly.

I am also worried that this will kind of increase the number of read/writes for our database.

Can someone please help me? Thanks in Advance

1 Answers1

1

The problem with this code is that I am not able to get listeners continuously with this code.

Why would you say that? This is what it does, it gets the data in real-time.

Can we use Android Services or something to continuously check for changes in the document or a field in the document.

If you want to get updates even if the user closes the app, indeed you need a service. For more info, please check my answer from the following post:

I am also worried that this will kind of increase the number of reads/writes for our database.

Yes, it will increase for sure the number of reads/writes if you continue to keep the listener active. So for not paying extra reads/writes, you should remove the listener according to the life-cycle of your activity as explained in my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I will be using 2 devices for this matter. I my app, I have an edittext and a button. In the 1st device, when I enter something and click the button, that data should go to firestore. While I upload something to firestore, the 2nd device should get to know and give a toast saying 'You have upload ---text--- to firestore just now'. How do I do this? And, it should be realtime as well. –  Jun 18 '21 at 08:38
  • What you are looking for can be achieved with what you already have. I mean using the `addSnapshotListener()`. It doesn't matter how many devices are listening to the same document, each one of them will be notified with the changes. To display a Toast the second device should also have the app in the foreground, right? – Alex Mamo Jun 18 '21 at 08:42
  • Yes, I think so. But will the ```adSnapshotListener()``` be continuous (running in the background continuously)? –  Jun 18 '21 at 09:33
  • Yes, it will, only if don't **explicitly** remove it. But you also should take into consideration that at some point in time the Android OS might close your app in favor of other apps. – Alex Mamo Jun 18 '21 at 09:36
  • So, can I place that piece of code in the ```onCreate``` method? –  Jun 18 '21 at 09:49
  • Yes, you can add wherever is needed, even in onCreate(). – Alex Mamo Jun 18 '21 at 09:53