Auto increment value in Firebase

How to automatically increase the value stored in Firebase from an Android client?

Currently: declare int id = 1 . When I increase, I see that the values ​​are 2, 3, etc. Saved. This is good, but when I re-run the project, id is set to 1 again.

I want it to behave like a static variable, so I can create an identifier that will go from 1 to infinity without a reboot.

UPDATED REFUSED

I used the following to pass a Firebase link and a string to the incrementCounter function.

 if(language_chosen.equalsIgnoreCase("english")) { Firebase publRef = f.child("Language").child("English").child("Message"); Firebase newPublRef = publRef.push(); writeMsgActivity demo = new writeMsgActivity(); demo.incrementCounter(newPublRef,the_msg_enter); } 

Now I am trying to use the passed link and string in public void incrementCounter(Firebase publref,String my_msg) in the oncomplete method, but this gives me an error.

  public void incrementCounter(Firebase publref,String my_msg) { publref.runTransaction(new Transaction.Handler() { @Override public Transaction.Result doTransaction(final MutableData currentData) { if (currentData.getValue() == null) { currentData.setValue(1); } else { currentData.setValue((Long) currentData.getValue() + 1); } return Transaction.success(currentData); } public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) { if (firebaseError != null) { System.out.println("Firebase counter increment failed."); } else { System.out.println("Firebase counter increment succeeded."); Map<String, Object> publ = new HashMap<String, Object>(); publ.put("pubMsg", my_msg); publ.put("id",currentData); publref.setValue(publ); } } }); } 

UPDATE DECISION

  final Firebase upvoteref = new Firebase("https://shareurday.firebaseio.com/Message/"+msg_id+"/upvotes"); upvoteref.runTransaction(new Transaction.Handler() { @Override public Transaction.Result doTransaction(final MutableData currentData) { if (currentData.getValue() == null) { currentData.setValue(1); } else { currentData.setValue((Long) currentData.getValue() + 1); } return Transaction.success(currentData); } public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) { if (firebaseError != null) { System.out.println("Firebase counter increment failed."); } else { System.out.println("Firebase counter increment succeeded."); } } }); 

The msg_id variable is a random generated identifier from push.

+10
java android auto-increment firebase firebase-realtime-database
source share
1 answer

Here's an example method that increments a single counter. The basic idea is that you either create a record (set it to 1) or mutate an existing record. Using a transaction ensures that if multiple clients try to increase the counter at the same time, all requests will ultimately be successful. From the Firebase documentation (emphasis mine):

Use our transaction function when dealing with complex data that may be corrupted by concurrent updates.

 public void incrementCounter() { firebase.runTransaction(new Transaction.Handler() { @Override public Transaction.Result doTransaction(final MutableData currentData) { if (currentData.getValue() == null) { currentData.setValue(1); } else { currentData.setValue((Long) currentData.getValue() + 1); } return Transaction.success(currentData); } @Override public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) { if (firebaseError != null) { Log.d("Firebase counter increment failed."); } else { Log.d("Firebase counter increment succeeded."); } } }); } 
+16
source share

All Articles