【问题标题】:Transaction causing activity to recall Firebase Android导致活动调用 Firebase Android 的事务
【发布时间】:2015-03-13 00:01:30
【问题描述】:

当我点击我的 rate_btn 来启动这个交易功能。

它工作正常,但在此过程中它会重新运行我的课堂活动一次(每次使用事务时我的课堂活动都会重新运行),因此会重置我的 msg_ID 等所有内容。

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

这是一个场景:

所以当点击这个“评分”按钮时,它会进行评分 在当前的 msg_ID 上,但是当我再次点击它时,它的评分不同 msg_ID 因为我得到了随机的 msg_ID。我认为这是由于 我调用 onComplete 方法。

当我点击 rate_btn

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

有什么想法吗?

【问题讨论】:

    标签: java android transactions firebase


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 2013-05-08
      • 1970-01-01
      相关资源
      最近更新 更多