1

I have many clients (android devices) that need to update the same resource. The problem is that some of the devices experience offline state. And then I have the following problem:

  • Device A is offline.
  • Device A updates Resource1 (R1) (no changes are transmitted to the firebase server)
  • Device B updates R1. This is the correct update chronologically speaking.
  • Device A becomes online, and all his old updates override the latest and the correct updates from device B.

Firebase docs say:

Firebase apps automatically handle temporary network interruptions. Cached data is available while offline and Firebase resends any writes when network connectivity is restored.

I think if somehow I will prevent firebase from sending all the request online it will solve my problem. I also read about maybe using dates (but then I need to rely on the time of each device, and any case every setvalue request will take more time so I will check the current update datetime on the server)

Maybe anyone had a similar problem?

thanks

HS1
  • 608
  • 2
  • 10
  • 28
  • It's hard to say without seeing the actual write operations. But most likely you **can** secure this with security rules. An example of advanced validations like that can be found here:https://stackoverflow.com/questions/37954217/is-the-way-the-firebase-database-quickstart-handles-counts-secure/37956590 – Frank van Puffelen Nov 21 '17 at 16:21
  • Thank you for your feedback. I think the security is not the issue here. Both of the devices are allowed to update. But in case one of them was offline when tried to update, and another one updated already, I don't want the offline changes to be applied. – HS1 Nov 21 '17 at 18:25
  • This requires the server to validate the write operation before committing it to the database. The only two options for that are using a server-side API (e.g. with Cloud Functions), or by using Firebase's security rules (which are evaluated server-side). – Frank van Puffelen Nov 21 '17 at 20:53

1 Answers1

0

To solve your problem, you only need to have a unique identifier between your records. For this i recomand you using push() method, which generates a unique id based on time. If want to display those values in a UI, just use a query and orderByKey() method and your problem will be solved.

Firebase apps automatically handle temporary network interruptions.

Firebase SDKs handle network interruptions automatically. We don't need to enable disk persistence for that. Enabling disk persistence ensures that the data also survives application restarts.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thank you for your feedback. I can't use unique keys here, because this resource should be updated by all of the devices. I just don't want outdated changes to be applied when the device is online again. – HS1 Nov 21 '17 at 18:26
  • There will be no outdated changes because each change will be updated on Firebase server once you are back online using coreponding pushed id, which as i said, its uniqueness is based on time. But rememebr, those push IDs are generated entirely on the client without consultation with the server. So that's why there will be no data that would be overridden. – Alex Mamo Nov 21 '17 at 18:30
  • @AlexMamo Firebase SDKs handle network interruptions automatically. You don't need to enable disk persistence for that, both the listened-to data and pending writes are cached in memory automatically (you can't even disable that). Enabling disk persistence ensures that the data also survives application restarts. – Frank van Puffelen Nov 21 '17 at 20:55