导致 activity 召回 Firebase Android 的交易
Transaction causing activity to recall Firebase Android
当我单击 rate_btn 以启动此 t运行saction 功能时。
它工作正常但在这个过程中它重新运行我的classactivity一次(我的classactivity重新运行s 每次使用 t运行saction)因此像我的 msg_ID 一样重置一切。
例如,我的 msg_ID 更改(由于使用 运行dom)每次 class activity 是调用,所以当我 运行 t运行saction 它工作但在 return 它 运行 class activity 也因此改变我的 msg_ID还有
这是一个场景:
so when click this "rate" button it does the rating
on the current msg_ID but when I click on it again, it rates a different
msg_ID because of my get random msg_ID. I think this is caused when
I call the onComplete method.
当我点击rate_btn
现在 class 重新 运行 我再次点击 rate_btn
它投了第一个 msg_id 然后当我再次点击 rate_btn 它投了第一个第二个键(因为它重新调用了 class activity 和 msg_id 改变了)
这是 t运行saction 的代码:
rate_btn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Firebase upvoteref = new Firebase("https://myapp.firebaseio.com/"+msgID+"/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.");
}
}
});
}
这是检索 运行dom id 的代码:
String msgID; //this variable is declare at the start of the class as a global variable.
Firebase ref = new Firebase("https://myapp.firebaseio.com/"+msgID);
ref.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
Iterable<DataSnapshot> ds = snapshot.getChildren();
//getting maximum number of children
long allNum = snapshot.getChildrenCount();
int maxNum = (int)allNum;
//getting the random integer from number of children
int randomNum = new Random().nextInt(maxNum);
Iterator<DataSnapshot> ids = ds.iterator();
int count = 0;
//has next will check if there are values in the next iteration , while count is used as a position substitute.
while(ids.hasNext() && count < randomNum) {
ids.next();
count ++; // used as positioning.
}
Map<String, Object> newPost = (Map<String, Object>) ids.next().getValue(); // ids will take the value in the iterator (iterate at key)
//getting the message from the key
msgID = newPost.get("id").toString();
}
有什么想法吗?
由于每次调用事务时消息 ID 都会更改,这是因为 addValueEventListener
(它在调用事务时接受更改)。因此,为了解决这个问题,我添加了 addListenerForSingleValueEvent
而不是 addValueEventListener
并且它起作用了。
例如:
ref.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
Iterable<DataSnapshot> ds = snapshot.getChildren();
//getting maximum number of children
long allNum = snapshot.getChildrenCount();
int maxNum = (int)allNum;
//getting the random integer from number of children
int randomNum = new Random().nextInt(maxNum);
Iterator<DataSnapshot> ids = ds.iterator();
int count = 0;
//has next will check if there are values in the next iteration , while count is used as a position substitute.
while(ids.hasNext() && count < randomNum) {
ids.next();
count ++; // used as positioning.
}
Map<String, Object> newPost = (Map<String, Object>) ids.next().getValue(); // ids will take the value in the iterator (iterate at key)
//getting the message from the key
msgID = newPost.get("id").toString();
}`
当我单击 rate_btn 以启动此 t运行saction 功能时。
它工作正常但在这个过程中它重新运行我的classactivity一次(我的classactivity重新运行s 每次使用 t运行saction)因此像我的 msg_ID 一样重置一切。
例如,我的 msg_ID 更改(由于使用 运行dom)每次 class activity 是调用,所以当我 运行 t运行saction 它工作但在 return 它 运行 class activity 也因此改变我的 msg_ID还有
这是一个场景:
so when click this "rate" button it does the rating on the current msg_ID but when I click on it again, it rates a different msg_ID because of my get random msg_ID. I think this is caused when I call the onComplete method.
当我点击rate_btn
现在 class 重新 运行 我再次点击 rate_btn
它投了第一个 msg_id 然后当我再次点击 rate_btn 它投了第一个第二个键(因为它重新调用了 class activity 和 msg_id 改变了)
这是 t运行saction 的代码:
rate_btn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Firebase upvoteref = new Firebase("https://myapp.firebaseio.com/"+msgID+"/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.");
}
}
});
}
这是检索 运行dom id 的代码:
String msgID; //this variable is declare at the start of the class as a global variable.
Firebase ref = new Firebase("https://myapp.firebaseio.com/"+msgID);
ref.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
Iterable<DataSnapshot> ds = snapshot.getChildren();
//getting maximum number of children
long allNum = snapshot.getChildrenCount();
int maxNum = (int)allNum;
//getting the random integer from number of children
int randomNum = new Random().nextInt(maxNum);
Iterator<DataSnapshot> ids = ds.iterator();
int count = 0;
//has next will check if there are values in the next iteration , while count is used as a position substitute.
while(ids.hasNext() && count < randomNum) {
ids.next();
count ++; // used as positioning.
}
Map<String, Object> newPost = (Map<String, Object>) ids.next().getValue(); // ids will take the value in the iterator (iterate at key)
//getting the message from the key
msgID = newPost.get("id").toString();
}
有什么想法吗?
由于每次调用事务时消息 ID 都会更改,这是因为 addValueEventListener
(它在调用事务时接受更改)。因此,为了解决这个问题,我添加了 addListenerForSingleValueEvent
而不是 addValueEventListener
并且它起作用了。
例如:
ref.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
Iterable<DataSnapshot> ds = snapshot.getChildren();
//getting maximum number of children
long allNum = snapshot.getChildrenCount();
int maxNum = (int)allNum;
//getting the random integer from number of children
int randomNum = new Random().nextInt(maxNum);
Iterator<DataSnapshot> ids = ds.iterator();
int count = 0;
//has next will check if there are values in the next iteration , while count is used as a position substitute.
while(ids.hasNext() && count < randomNum) {
ids.next();
count ++; // used as positioning.
}
Map<String, Object> newPost = (Map<String, Object>) ids.next().getValue(); // ids will take the value in the iterator (iterate at key)
//getting the message from the key
msgID = newPost.get("id").toString();
}`