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.
java android auto-increment firebase firebase-realtime-database
nothingness
source share