gpt4 book ai didi

java - 交易导致 Activity 召回 Firebase Android

转载 作者:太空宇宙 更新时间:2023-11-03 11:09:09 28 4
gpt4 key购买 nike

当我点击我的rate_btn 启动此交易功能时。

它工作正常,但在此过程中它再次重新运行我的类 Activity (每次使用事务时我的类 Activity 都会重新运行)因此重置所有内容,如我的 msg_ID

例如,每次调用此类 Activity 时,我的 msg_ID 都会更改(由于使用随机),因此当我运行事务时它有效但作为返回,它也运行了类 Activity ,因此改变了我的 < strong>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

vote up the first key

现在类(class)重新开始,我再次点击 rate_btn

vote up second key

它投票赞成第一个 msg_id 然后当我再次点击 rate_btn 它投票赞成第二个键(因为它重新调用了类 Activity 和 msg_id 已更改)

这是交易代码:

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.");
}
}
});
}

下面是获取随机 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();
}`

关于java - 交易导致 Activity 召回 Firebase Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29022566/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com