导致召回Firebase Android的交易活动

时间:2015-03-13 00:01:30

标签: java android transactions firebase

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

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

例如,每次调用此类活动时,我的 msg_ID 会发生更改(由于使用随机),因此当我运行事务时它会起作用,但作为回报,它还运行了类活动,因此更改了我的< strong> msg_ID 以及。

这是一个场景:

  

所以当点击这个&#34;率&#34;按钮它做了评级   在当前的 msg_ID 上,但当我再次点击它时,它会有不同的评分    msg_ID 因为我的随机 msg_ID 。我认为这是造成的   我调用了onComplete方法。

点击 rate_btn

vote up the first key

现在课程重新开始,我再次点击 rate_btn

vote up second key

它对第一个 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<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();
        }

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

由于每次调用事务时消息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();
    }`