I’m looking for a function of the api that incrementing/ decrementing object column atomicly,
is there something like that?
Yes, there is. Here’s the doc for Java/Android (use the dropdown in the upper right corner to change the language of the API):
can i save a counter in a data table or anywhere else,
for access to the same counter by multiple clients (app users)?
if not, is the counter only available in the scope in which it was created?
The same counter can be accessed by any client-type, they are cross-platform since the actual value is maintained by the server.
“saving a counter” does not make whole lot of sense to me. You can save any specific atomic value of the counter, but the actual counter would be handled within our app server.
the reason i want to save the counter is this:
let’s say i have a table with comments, where each cell represents one comment.
each comment has the user who wrote it (his objectId) as a property.
i also have a like button in each cell, for liking that comment.
also every user has a property lets call it sum, that saves the overall number of
likes that specific user got.
now the problem is:
if someone likes two comments by the same user and does so fast enough,
the function that is triggered by the like retrieves the user who wrote the comment,
adds 1 to his sum property and saves the changes.
but the problem is that before the changes are saved the second like triggered another
fetch of the user with the old sum and again adds 1 to it.
so if sum was 0 after two likes it is 1.
same can happen if two different users like a comment by the same third user.
The way our implementation of the atomic counters works is it guarantees thread safety. As a result, for the scenario you described, if there are two or more users who like a comment at the time, each “like” would be counted atomically. Therefore, the concept of “saving a counter” would essentially mean saving the counter name and accessing its value on as-needed basis.
ok so in order to save the counter name i need to create a counter at some point.
let’s say when a new comment is created i will also create a counter and i will save
its name in the comment object.
my question now is: if the user who created the comment now exits the app or maybe
goes to a completely different part of the app, where will the counter actually be?
because now a different user will want to like that new comment, so the user
will try to increment the counter who’s name we saved in the comment object.
how can he actually get that counter?
my question now is: if the user who created the comment now exits the app or maybe goes to a completely different part of the app, where will the counter actually be?
The counter will be in the cloud - it is stored on our servers.
because now a different user will want to like that new comment, so the user will try to increment the counter who's name we saved in the comment object. how can he actually get that counter?
Using the same API as before. You reference a counter by its name.