I have an Android app, that keeps track of some items and their quantities.
What I need is a way for the user to modify data on multiple devices while being offline, with proper synchronization order.
I don't know how to best explain it, so I'll give an example:
Can this situation be prevented natively in Firebase, or should I, for example, use timestamps on items myself, and replace it only if newer?
Thanks.
Adding timestamp field to every item in database and writing security rule in firebase seems to work.
The thing to consider is, when offline, the only timestamp I can get is from the device, not from the server, so it can sometimes lead to overwrite of data that we don't want. Still it works good enough for this project.
Below is the database rule needed to check those timestamps:
"$name" : {
".write": "!data.exists() || (newData.child('timestamp').isNumber() && data.child('timestamp').val() <= newData.child('timestamp').val())"
}
It says: "you can write if there's no data, or stored data has older timestamp that new request"
Important thing to remember while making those is, that parent nodes overwrite rules in children. That means any authentication of write should be done on this level too.