How do i set up a counter for user ID in Android

Hi, I need to set a field in the user schema as user id and increment it everytime a new user registers.

I read about the counters api but am not able to understand how to implement it.

Urgent help needed.

Hello! you can use “before handler” for your idea

when the user registers , get the id of last user in the table, and increment that value and then set that value for new user

But what about if two or even three users are registering at the same time? They all will get the same ID if you use your solushion. So it is better to use Atomic counters. There is documentation https://backendless.com/documentation/utilities/android/ut_atomic_counters_api_android.htm

Yes, i agree with you. But cant understand how to create that counter!! :frowning: even after reading the doc. Cant understand where they are storaged…

Valeria, counter is created automatically on first call to it.

Hm…Alexandr, how can i see the list of counters?

I use this code, but it doesnt seem that it is working…

IAtomic<Integer> myCounter = Backendless.Counters.of( "id", Integer.class );
int counterValue = myCounter.incrementAndGet();




Comments comment = new Comments();
comment.setId(counterValue);
comment.setIdNews(idNews);
comment.setText(text);
comment.setUserEmail(userEmail);
Backendless.Data.save(comment);

Can you run this code twice and check created comments?
And please clarify what do you mean under “doesn’t work”?

Hm… Alexandr, you have a hiden power! Now it works perfect! As i wanted. :slight_smile: Its really strange :slight_smile:

Thank you for your attention to my problem !! :slight_smile:
And i use that code with try catch. And i said “doesnt work” i meant that i went to catch block.

Regards, Valeria