导致召回Firebase Android的交易活动

当我点击我的rate_btn来启动此交易function时。

它工作正常,但在此过程中它再次重新运行我的类活动(每次使用事务时我的类活动重新运行)因此重置像我的msg_ID这样的所有内容。

例如,每次调用此类活动时,我的msg_ID都会更改(由于使用随机),因此当我运行事务时它会起作用,但作为回报,它还运行了类活动,因此也改变了我的msg_ID

这是一个场景:

因此,当点击这个“速率”按钮时,它会对当前的msg_ID进行评级,但是当我再次点击它时,由于我获得了随机的msg_ID ,它会对不同的msg_ID进行评级 。 我认为这是在我调用onComplete方法时引起的。

当我点击rate_btn

投票第一把钥匙

现在class级重新运行,我再次点击rate_btn

投票第二关键

它对第一个msg_id进行了投票,然后当我再次点击rate_btn时它会对第二个键进行投票(因为它重新调用了类活动并且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 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 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 newPost = (Map) 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 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 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 newPost = (Map) 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(); }`