事务导致活动召回 Firebase Android [英] Transaction causing activity to recall Firebase Android

查看:19
本文介绍了事务导致活动召回 Firebase Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击我的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 都会更改(由于使用随机),因此当我运行事务时,它可以工作,但作为回报,它运行了类活动,因此也更改了我的 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

现在课程重新运行,我再次点击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)

交易代码如下:

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的代码:

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

有什么想法吗?

推荐答案

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

这篇关于事务导致活动召回 Firebase Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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