Firebase transaction operation to increase/decrease value

firebase

#1

Hi Happybuilders,
I’m using Firebase and I’d like to increase/decrease a global counter on users’ input (a sort of social “like”).
Firebase handles concurrent modifications with transaction operation: you send to Firebase a request to increase/decrease the current value stored in Firebase, without sending the specific value from the app that could be outdated.

Is there a way to do this with appybuilder?
Otherwise you have to:
a- get the value
b- on user request, re-get the value (or keep it updated as possible we onchange)
c- send the increase request
that probably will work for most cases, but if this can be implemented in a stronger way since the beginning I’d like to do it.

PS: Another solution could be a Firebase function,but I’d like to avoid a server implementation

thanks
cheers
Goblin


#2

Get Value, Value id not thre option

Use this


#3

image


#4

What you want to do is actually:

Screenshot%20(68)


#5

Goblin,

To answer your actual question, (how to increase value WITHOUT a GetValue) to increase a counter without retrieving the data first, you can try using the web component, and send a REST API request.

https://firebase.google.com/docs/database/rest/save-data

The above link gives an example of how to use this for an upvote function, in case multiple people are upvoting at the same time.


#6

thanks you all for the suggestions, for now I go simply with getValue+1
but I will study the REST API solution suggested by Cian_O_Sullivan that should be good to avoid concurrency


#7

Mark my solution as the solution so people can find it easier :slight_smile::blush:


#8

I checked the REST API solution, but I’m not able to reach my goal.
I set up the web request: it’s less simple that the Firebase component, but with this I think you can:

  • put data type “number” to firebase and not only strings, so you can write more easy validate rules on FireB.
    In my experience, the FireB component writes to FireB always strings: this means that
    for example: NewData.val() === Data.val()+1 will never work.
    If on FireB the data is a string, you need to write rules with regular expression (not sure if there is a way to do math checks)

  • hide FireB url and token in the app package, if the obfuscated text works
    (but with a network tracking tool the web request will be visible I suppose, so obfuscation is useless?)

Anyway, I didn’t find a way to increment without knowing the starting value.
Probably the solution is a server function on FireB.