Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

java - Transaction causing activity to recall Firebase Android

When I click my rate_btn to start this transaction function.

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.

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.

Here's a scenario:

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.

When I click the rate_btn

vote up the first key

Now the class re-run and I click on the rate_btn again.

vote up second key

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)

Here's the code for transaction:

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

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

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...