Decrease (Update) column on Users table


I will have the concept of ‘actions’. And when user taps to train, edit something, etc. those actions will be decreasing from him and after the day is over they will be reset back to max.

My problem is, how can I decrease that actions column (per User) like it was an atomic counter, because user can perform X requests at same time and I need to make sure that proper amount of actions are decreased.

Thanks for the help,
Telmo Cardoso

I think you can make this logic based on Atomic Counter.

You can have one atomic counter per user and a business logic timer which runs after the day and resets that counters.

Now, if you still want to have this value in Users column, create a custom event handler and call it every when user perform some action. This handler will incrementAndGet the related user’s atomic counter and save it to the Users table into user actions column.
Then, no matter how many actions are performed at the same time for a single user, you will get a valid column value

Thanks for reply Vitaly,

My “real” case is more complex, because I have “normal” actions per user, plus membership actions, etc. But still I think your approach of have one atomic counter per user will work.

Now, my “second” question is I will prefer to use a decrementing counter and I seen that I can call Backendless::$Counters->addAndGet and providing a negative value it works the way I want (P.S. - It will even go to negative values).

Because there isn’t any decrement by N, can I use the increment by N with negative values without any problem? Like wont it be protected in the future because it may be considered a “bug” :)?

Thanks for support.

Hi Telmo,

Yes, your approach is totally fine and won’t be restricted in future.