交易活动造成召回的Andr​​oid火力地堡 [英] Transaction causing activity to recall Firebase Android

查看:238
本文介绍了交易活动造成召回的Andr​​oid火力地堡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击我的 rate_btn 以启动该交易功能。

When I click my rate_btn to start this transaction function.

它工作正常,但在这个过程中重新运行我的班级活动一次(我的班级活动重播,每次交易时使用),因此像我的 MSG_ID 重置一切。

It works fine but in the process it re-run my class activity one more time (my class activity re-runs every time transaction is used) therefore resetting everything like my msg_ID.

例如,我的 MSG_ID 的变化(由于采用随机)每到这个类的活动就叫做时间,所以当我运行交易它的​​工作原理,但换来的却跑了班级活动也因此改变我的< STRONG> MSG_ID 以及。

For example, my msg_ID changes (due to using random) every time this class activity is called, so when I run transaction it works but in return it ran the class activity also therefore changing my msg_ID aswell.

下面是一个情景:

所以,当点击这个速度按钮,它确实评级
  对当前的 MSG_ID 但是当我再次点击它,它率不同
   MSG_ID ,因为我看到一些随机的 MSG_ID 。我认为这是造成时
  我称之为的onComplete方法。

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

When I click the rate_btn

现在,类重新运行,我点击 rate_btn 试。

这票了第一个 MSG_ID ,然后当我点击 rate_btn 它再次投票了第二个关键(因为它重新被称为课堂活动,并在 MSG_ID 更改)

It votes up the first msg_id then when I click the rate_btn again it votes up the second key (Because of it re-called class activity and the msg_id changed)

这里的code事务:

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

下面是code检索随机ID:

Here is the code for retrieving the random 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 和它的工作。

Since the Message ID changes every every time transaction is called, this is because of addValueEventListener (it accept the changes when calling transaction). Therefore to solve this I add addListenerForSingleValueEvent instead of addValueEventListener and it worked.

例如:

 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();
    }`

这篇关于交易活动造成召回的Andr​​oid火力地堡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆