在 Firebase 中自动增加一个值 [英] Auto-increment a value in Firebase
问题描述
如何从 Android 客户端自动递增 Firebase 中存储的值?
How do I auto increment a value stored in Firebase from an Android client?
目前:我声明 int id = 1
.当我递增时,我看到值 2、3 等被存储.这很好,但是当我重新运行该项目时,id
再次设置为等于 1.
Currently: I declare int id = 1
. When I increment, I see the values 2, 3 etc. being stored. That's fine, but when I re-run the project, id
is set equal to 1 again.
我希望它表现得像一个静态变量,所以我可以创建一个从 1 到无穷大的 id,而无需重置.
I want it to behave like a static variable, so I can create an id which will go from 1 to infinity without resetting.
更新失败
我使用以下代码将 Firebase 引用和一个字符串传递给函数 incrementCounter.
I used the following to pass the Firebase reference and a string to the function incrementCounter.
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);
}
现在我尝试在 oncomplete 方法中使用 public void incrementCounter(Firebase publref,String my_msg)
处传递的引用和字符串,但它给了我一个错误.
Now I try to use the passed reference and string at public void incrementCounter(Firebase publref,String my_msg)
in the oncomplete method but it 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);
}
}
});
}
更新解决
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.");
}
}
});
变量 msg_id 是从推送中随机生成的 id.
The variable msg_id is the random generated id from push.
推荐答案
这是一个递增单个计数器的示例方法.关键思想是您要么创建条目(将其设置为等于 1)要么改变现有条目.在此处使用事务可确保如果多个客户端同时尝试增加计数器,则所有请求最终都会成功.来自 Firebase 文档(重点是我的):
Here's an example method that increments a single counter. The key idea is that you are either creating the entry (setting it equal to 1) or mutating the existing entry. Using a transaction here ensures that if multiple clients attempt to increment the counter at the same time, all requests will eventually succeed. From the Firebase documentation (emphasis mine):
在处理复杂数据时使用我们的事务功能并发更新可能会损坏
Use our transactions feature when working with complex data that could 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.");
}
}
});
}
这篇关于在 Firebase 中自动增加一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!