[英]dataSnapshot if else keep looping
The below states that if the month is equal to the value in Firebase , it will add "monthamt" to the existing month.下面说明如果月份等于Firebase中的值,它将向现有月份添加“monthamt”。 Else it will push a new key for the new month and store "monthamt" into it.
否则它将为新的月份推送一个新密钥并将“monthamt”存储到其中。 But in my case, even thou if(a month is equal to the existing in Firebase , it will still push a new key with the same month).
但就我而言,即使你如果(一个月等于Firebase中的现有月份,它仍会在同一个月推送一个新密钥)。
ds = mDatabase.getReference().child("Users").child(uid).child("summary");
ds.orderByKey().addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Calendar c = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("ddMMyy");
SimpleDateFormat fm = new SimpleDateFormat("MM");
String month = (String) dataSnapshot.child("month").getValue();
double mvalue = Double.valueOf(String.valueOf(dataSnapshot.child("monthamt").getValue()));
mvalue = roundOff(mvalue);
if (fm.format(c.getTime()).equals(month)) {
String key1 = dataSnapshot.getKey();
ds.child(key1).child("monthamt").setValue(roundOff(mvalue + amt) + "");
}
else if(!fm.format(c.getTime()).equals(month)) {
String key = ds.push().getKey();
Calendar b = Calendar.getInstance();
ds.child(key).child("month").setValue(fm.format(b.getTime()));
ds.child(key).child("monthamt").setValue(amt + "");
dt.child("Bill").child(k).child("gtranid").setValue("1");
}
}
Below is what my Firebase looks like:下面是我的 Firebase 的样子:
Your approach is wrong.你的做法是错误的。 Try something like this.
尝试这样的事情。
String key;
int currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
ds.addListenerForSingleValueEvent(new ValueEventListener () {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data: dataSnapshot.getChildren()){
int month = data.child("month").getValue();
if (month == currentMonth) {
// update existing one
key = data.getKey();
break;
}
}
}
});
if (key != null) {
double mvalue = data.child("monthamt").getValue();
mvalue = roundOff(mvalue);
ds.child(data.getKey()).child("monthamt").setValue(roundOff(mvalue + amt) + "");
} else {
key = ds.push().getKey();
ds.child(key).child("month").setValue(currentMonth);
ds.child(key).child("monthamt").setValue(amt);
dt.child("Bill").child(k).child("gtranid").setValue("1");
}
There's quite a few problems with your code.您的代码有很多问题。 Some of the first ones that jump out:
一些最先跳出来的:
Why are you storing numeric values as strings?为什么将数值存储为字符串? By doing so, you end up converting them repeatedly in your code, which makes that code longer and harder to maintain.
通过这样做,您最终会在代码中重复转换它们,这使得该代码更长且更难维护。 I recommend storing numbers as number in the JSON.
我建议将数字存储为 JSON 中的数字。
If the value you want to write to the database depends on a current value in the database, you need to use a transaction .如果要写入数据库的值取决于数据库中的当前值,则需要使用事务。
If you want months to be unique in the JSON, you should use them as the key of the node, instead of as a property in there.如果您希望月份在 JSON 中是唯一的,您应该将它们用作节点的键,而不是作为其中的属性。 I'd model your data as:
我将 model 您的数据作为:
"summary": { "month_10": 88, "month_11": 660 }
This has the advantage that you don't have to query to find the node for the month, as you already know its path:这样做的好处是您不必通过查询来查找该月的节点,因为您已经知道它的路径:
DatabaseReference monthRef = ds.child("month_"+mm);
Firebase nowadays has a built-in operation for incrementing values, so that you don't even need a transaction for this. Firebase 现在有一个用于递增值的内置操作,因此您甚至不需要为此进行交易。 It should be as simple as:
它应该很简单:
monthRef.setValue(ServerValue.increment(44));
This even works when the month node doesn't exist yet, as increment
will simply assume that it is 0 in that case.这甚至在月份节点尚不存在时也有效,因为在这种情况下
increment
将简单地假定它为 0。
I have figured it out, code as below, instead of using else to loop, I uses if to get month which is different than those in Firebase, and add a break to every if statement to prevent looping:我已经想通了,代码如下,我没有使用else循环,而是使用if获取与Firebase不同的月份,并在每个if语句中添加一个break以防止循环:
Calendar c = Calendar.getInstance();
final int currentMonth = c.get(Calendar.MONTH)+1;
ds.orderByChild("month").addListenerForSingleValueEvent(new ValueEventListener () {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for (DataSnapshot data: dataSnapshot.getChildren()){
String month = data.child("month").getValue().toString();
String convert1 = String.valueOf(currentMonth);
if (convert1.equals(month)) {
// update existing one
String key = ds.getKey();
double mvalue = Double.valueOf (String.valueOf(data.child("monthamt").getValue()));
mvalue = roundOff(mvalue);
ds.child(data.getKey()).child("monthamt").setValue(roundOff(mvalue + amt) + "");
break;
}
if(!convert1.equals(month)){
String key = ds.push().getKey();
ds.child(key).child("month").setValue(convert1);
ds.child(key).child("monthamt").setValue(amt + "");
break;
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.