Firebase同步会导致滞后 [英] Firebase syncing causes lag

查看:116
本文介绍了Firebase同步会导致滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,将Polymer与Firebase结合使用。我有一个旋钮元素,只是一个值存储在Firebase中的旋钮。聚合物观察者观察到旋钮的值,当值改变时更新火力点值。问题如下:当值改变为1时将其更新为火力点值。这会向所有其他地方发出更改事件。在其他地方,这使元素中的值被设置,因此观察者被触发。这个观察者导致火力值被重新设置。这反过来再次发射一个变化,等等...这会导致适应旋钮时的laggy行为。我可以做什么?

 < script> 

聚合物({
是:'disco-ccontrol',

属性:{
midiValue:{
type:Number,
value:0,
observer:'_valueChanged',
notify:true
},
channel:{
type:Number,
value :0
},
channelNumber:{
type:Number,
value:0
},
ref:{
type:Object ,
计算:'_computeRef(channel,channelNumber)'
}
},

_computeRef:函数(channel,channelNumber){

var ref = new Firebase(https://incandescent-inferno-8405.firebaseio.com/user/+ this.channel +'/'+ this.channelNumber);
ref.on(child_changed,函数(数据){
this.midiVal ue = data.val();
} .bind(this));

return ref;

$ b _valueChanged:function(){
var message = {value:this.midiValue,channel:this.channel,channelNumber:this.channelNumber};
if(this.ref){
this.ref.set(message);
}
}

});

< / script>


解决方案

  ref.on(child_changed,function(data){

您使用

  ref.once(child_changed,function(data){

仅在启动时从firebase获取值,之后您只更新firebase中的值。 / p>

当然取决于您的整体应用程序架构,但从您在此显示的内容看起来确实如此。


I am working on a project where I use Polymer in combination with Firebase. I have a knob element that is just a knob which value is stored in Firebase. The value of the knob is observed by a polymer observer, that when the value changes updates the firebase value.

The problem is the following : when the value is changed in one place it updates the firebase value. This emits a change event to all other places. In other places this makes the value in the element being set and hence the observer triggered. This observer causes the firebase value being set again. This in turn emits again a change, and so forth ... This causes laggy behaviour when adapting the knob. What can I do?

<script>

    Polymer({
        is: 'disco-ccontrol',

        properties: {
            midiValue: {
                type: Number,
                value: 0,
                observer: '_valueChanged',
                notify: true
            },
            channel: {
                type: Number,
                value: 0
            },
            channelNumber: {
                type: Number,
                value: 0
            },
            ref: {
                type: Object,
                computed: '_computeRef(channel, channelNumber)'
            }
        },

        _computeRef: function(channel, channelNumber) {

            var ref = new Firebase("https://incandescent-inferno-8405.firebaseio.com/user/"+this.channel+'/'+this.channelNumber);
            ref.on("child_changed", function(data) {
               this.midiValue = data.val();
            }.bind(this));

            return ref;
        },

        _valueChanged: function() {
            var message = { value: this.midiValue, channel: this.channel, channelNumber: this.channelNumber };
            if (this.ref) {
                this.ref.set(message);
            } 
        }

    });

</script>

解决方案

How about instead of

ref.on("child_changed", function(data) {

you use

ref.once("child_changed", function(data) {

to only get the value from firebase at startup & after that you are only updating the value in firebase.

Of course depends on your overall app architecture, but from what you show here it seems ok.

这篇关于Firebase同步会导致滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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