2

My app is mostly used outdoors, so there is a Firestore load problem if there is no internet. When a user comes back home, he will have wifi but the app is closed and will not sync. Again when the user goes out and opens the app, there might not be an internet connection and the loop goes on.

What is the best way to sync Firestore in the background? There is BroadcastReceiver option but it is stated that WorkManager should be used instead. Also, there is LiveData but I can't find a Java example, so... what is best? (an example would be nice)

EDIT I had to delete some of the things because my question "is not focused enough"!?!?!?!

Nemanja
  • 211
  • 6
  • 16
  • Removing multiple questions to only leave one is certainly an improvement, but the question that's remaining is still too broad and opinion based. You need to be much clearer about what you mean when you say "best". – cigien Aug 06 '21 at 00:02
  • Any solution to this @Nemanja? – Anoop Thiruonam Nov 09 '21 at 18:11

1 Answers1

2

so there is a Firestore load problem if there is no internet.

There should not be any problems when the user has no internet connection, and this is because Firestore has its own default caching mechanism:

For Android and iOS, offline persistence is enabled by default.

You say:

When the user comes back home, he will have wifi but the app is closed and will not sync.

It won't sync unless you are listening for real-time changes. Besides that, it's always recommended to remove the lister according to the life-cycle of your activity. So if you are using addSnapshotListener() you attach a listener that gets called for every change that takes place in your database. So this is happening also when your app is closed. Not removing the listener might produce unwanted bills. So it's mandatory to detach the listeners before the activity gets destroyed. For more info, please check the following links:

#1. It's not recommended to do that. However, JobDispatcher is old and deprecated, and no longer available, not the BroadcastReceiver class. But if need, you should use WorkManager instead. If you must absolutely use JobDispatcher, which I recommend against it, the source code is archived here. So please read about how to migrate from JobDispatcher to WorkManager.

#2.

Can I use test DB for production and change permissions to read-only? Does that test DB have an expiration date?

Each database has its own Security Rules. So you should choose to secure your data according to your needs. However, there is no expiration date involved, but you can use something like this:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.time < timestamp.date(2021, 08, 05);
    }
  }
}

To limit the access to the database until tomorrow, August 5th, 2021.

#3. You can use the database also without security rules. But most likely you should consider using them. Most likely you shouldn't use the username, nor the email address, but UID that comes from the authentication process.

#4. Yes, every CRUD operation counts against your free quota. And yes, there's a minimum of one document read, even if the query yields no results. So if your query doesn't return any results, you have to pay one document read.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • yes, there is cache but if I update db, user will not get updates, Ive been testing that for days now. Listener is detached when app closes! So is it OK to use work manager to check for wifi on/off and sync db? #3 well, as far as I saw, auth requires login with email!? Does it has to be email or can it be user name?#4 thx :) – Nemanja Aug 05 '21 at 15:48
  • *there is cache but if I update DB, the user will not get updates* That's normal since there is no internet connectivity. *Listener is detached when the app closes* That's good. *So is it OK to use work manager to check for wifi on/off and sync DB?* If that is your requirement, go ahead. That's why WorkManager exists. *as far as I saw, auth requires login with email!?* No, there are many ways in which you can authenticate the user. Not only email. But even if you are using the email, store the data in the DB under the UID, and not the email or username. Because the email address might change. – Alex Mamo Aug 06 '21 at 10:04
  • Data is not persisted if app is closed or the phone is switched off before connectivity is up. – Anoop Thiruonam Nov 09 '21 at 18:13
  • @AnoopThiruonam Data is persisted if the app is closed. Give it a try. I tested it ;) – Alex Mamo Nov 09 '21 at 20:46
  • 1
    @AlexMamo I tried so many times and it finally succeeded! Thanks to you I just went on trying out so much! – Anoop Thiruonam Dec 15 '21 at 16:56
  • @AnoopThiruonam Good to hear that, Anoop ;) – Alex Mamo Dec 15 '21 at 20:31