Firebase dataRef.transaction()会返回错误的快照 [英] Firebase dataRef.transaction() returns wrong snapshot

查看:112
本文介绍了Firebase dataRef.transaction()会返回错误的快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户点击#div1或#div2时为用户分配增量号码。来自不同设备的多个用户可以在没有问题的情况下使用以下代码:

  $(#div1)。click (function(event){
doBtn();
}

$(#div2)。


函数doBtn(){
counterRef.transaction(function(currentData){
if(currentData === null){
return 1 ;
} else {
//将counterRef增加1
return currentData + 1;
}
},function(error,committed,snapshot){
if(error){
} else if(!committed){
} else {
//在counterRef更新成功后,添加一个子项{user:userId,counter:xxxx }到ListRef
//(xxxx是增加的计数器编号)
ListRef.push({
user:userId,
计数器:snapshot.val()
});
}
});
}

计数器Ref.on('value',function(snapshot){
console.log(Current number is+ snapshot.val());
}

ListRef.on('child_added',function(snapshot){
console.log(Your number is+ snapshot.val()。counter); $但是,如果用户点击同一个按钮(或者点击




> >#div1
#div2 ),他们会得到如下结果:

< blockquote>

当前编号是1
当前编号是2
当前编号是3
当前编号是4
当前编号是5
当前编号号码是6
当前号码是7
当前号码是8



您的号码是1
您的号码是3
你的号码是3
你的号码是4
你的号码是5
你的号码是6
你的号码是8
你的号码是8


看来,函数(错误,提交,快照)中的快照值无法获得并发值从 counterRef 增加。



我是否使用 .transaction()错误?

请帮忙。

解决方案

不幸的是,基于@ Kato的再现,我认为这是基于我们当前实现的预期行为。基本上,如果您快速地(或在离线时)执行多个事务,那么我们最终会将它们分配到一起并作为一个批处理执行它们,并且最终的快照每一个得到的将是所有人跑完后的最终快照。



我知道这可能并不总是理想的。我们将研究是否可以在将来改变这种行为。


I'd like to assign an incremental number to users while they click on #div1 or #div2. Multiple user from different devices can get their subsequent number without problem with the following codes:

$("#div1").click(function(event) {
    doBtn();
}

$("#div2").click(function(event) {
    doBtn();
}

function doBtn() {
    counterRef.transaction( function(currentData) {
      if (currentData === null) {
        return 1; 
      } else {
        // increase the counterRef by 1
        return currentData + 1;
      }
    }, function(error, committed, snapshot) {
      if (error) {
      } else if (!committed) {
      } else {
        // upon counterRef updated successfully, add a child {user: "userId", counter: xxxx} to ListRef
        // (xxxx is the increased counter number)
        ListRef.push({
            user: "userId",
            counter: snapshot.val()
        });
      }
    } );
}

counterRef.on('value', function(snapshot) {
    console.log( "Current number is " + snapshot.val() );
}

ListRef.on('child_added', function(snapshot) {
    console.log( "Your number is " + snapshot.val().counter );
}

However, if the user click the same button (or clicking #div1 and #div2 alternatively) rapidly, they will get the following results:

Current number is 1 Current number is 2 Current number is 3 Current number is 4 Current number is 5 Current number is 6 Current number is 7 Current number is 8

Your number is 1 Your number is 3 Your number is 3 Your number is 4 Your number is 5 Your number is 6 Your number is 8 Your number is 8

It seems that the snapshot value in function(error, committed, snapshot) cannot get the concurrent value from counterRef while being increased.

Am I using the .transaction() wrong?

Please help.

解决方案

Unfortunately, based on @Kato's reproduction, I think this is expected behavior based on our current implementation.

Basically, if you do multiple transactions rapidly (or while you're offline), we'll end up batching them together and performing them as a single batch, and the final snapshot each one gets will be the final snapshot after all of them ran.

I understand this may not always be ideal. We'll look into whether we could perhaps change this behavior in the future.

这篇关于Firebase dataRef.transaction()会返回错误的快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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