Is there an Firebase Realtime Database equivalent to the increment method available in Firestore?

Viewed 816

Despite a ton of searching, I can't find any method equivalent in the Firebase Realtime Database to the increment method available in Firestore.

The problem I am trying to solve is that I have a ticketing app which increments a counter on Firebase for attendees at the event; however if a user went offline and was registering attendees with the persistent database, while another user who is online was also doing so, when the first user comes back online, they overwrite the value for the counter using their stored value plus any additional attendees they validated before coming back online. This does not take into account the fact that the value of the counter had changed before they came back online. There will be 6 people validating event tickets, so this value will always be wrong if someone reconnects and overwrites values registered by the other users. So if there is an increment method like this one:

increment = firebase.firestore.FieldValue.increment(1);

Can you please let me know what it is? If not, how would you approach my issue with data persistence on the device in Swift?

N.B. I am using the Realtime Database as opposed to Firestore because it was recommended for speed and data use when you are in a situation where you are doing a ton of small updates to the database -- which obviously will be the case when you are processing tickets for thousands of people.

4 Answers

There's a new method ServerValue.increment()in firebase Swift SDK

It's better for performance and cheaper since no round trip is required.

API Docs here

Usage example:

firebase.database()
    .ref('somePath')
    .child('someCounter')
    .set(firebase.database.ServerValue.increment(1))

Or you can decrement, just put -1 as function arg like so:

firebase.database()
    .ref('somePath')
    .child('someCounter')
    .set(firebase.database.ServerValue.increment(-1))

The only safe way to increment a value (using client code only) in Realtime Database is using a transaction, which requires the client app to be online.

If you require offline access which would eventually tally up a count when the app comes back online, you can use a Cloud Functions trigger. The strategy here would not be for the client app to increment a number. It would be instead to add or remove a per-user child value. The function code would then look at the value (maybe either true or false) and increment or decrement the counter in response the location being written.

Here's a video series that discusses how to get started, and eventually how to implement a child counter in with Cloud Functions against changes to Realtime Database. You could do something similar.

https://firebase.google.com/docs/functions/video-series/

Realtime Database does seem to have this feature now. The API reference is here: https://firebase.google.com/docs/reference/js/firebase.database.ServerValue#increment

Basically in a ref.update() you will provide database.ServerValue.increment(n) on the field you wish to increment.

For example:

ref.update({
  counter: database.ServerValue.increment(1)
});

You may also provide a negative value to decrement:

ref.update({
  counter: database.ServerValue.increment(-1)
});

you add:

ref.update({
  counter: database.ServerValue.increment(1)
});

If you wanted to subtract

ref.update({
  counter: database.ServerValue.increment(-1)
});
Related