Firebase 删除节点数组会减慢组件渲染速度
Firebase Delete node array slow down the component rendering
我有一个 Angular7 组件连接到 Firebase 中的实时数据库。
组件绑定到结构化项数组调用"signals",每次向数组添加新项时,组件都会进行更新。
一切正常。
问题出在清空数组的时候...
在这种情况下,实时数据库似乎一个一个地删除数组项,并且对于每个删除的项,它继续将整个数组推送到组件...
这是代码:
绑定部分
ngOnInit() {
this.db.list<RHSignal>('signals', ref => ref).valueChanges().subscribe(res => {
this.listSignal = res;
})
}
删除数组的函数
ClearSignals(){
this.db.object('signals').remove();
alert('Signals has been removed !!!');
}
如您所见,"signal" 节点(可以包含 200 多个结构化项目)被一个命令删除,但绑定继续发送每个删除的数组项目的数据。
这会导致页面变得非常慢或被阻塞。
我需要一个更快更好的方法来避免这个问题。
感谢支持
终于找到了解决办法:
通过这个阅读官方文档后link:
https://firebase.google.com/docs/database/web/read-and-write
我发现:
Delete data
The simplest way to delete data is to call remove() on a reference to
the location of that data.
You can also delete by specifying null as the value for another write
operation such as set() or update(). You can use this technique with
update() to delete multiple children in a single API call.
所以我把清除方法改成这样:
ClearSignals(){
var updates = {};
updates['/signals'] = {};
this.db.database.ref().update(updates);
alert('Signals has been removed !!!');
}
现在速度真快!
我有一个 Angular7 组件连接到 Firebase 中的实时数据库。
组件绑定到结构化项数组调用"signals",每次向数组添加新项时,组件都会进行更新。
一切正常。
问题出在清空数组的时候... 在这种情况下,实时数据库似乎一个一个地删除数组项,并且对于每个删除的项,它继续将整个数组推送到组件...
这是代码:
绑定部分
ngOnInit() {
this.db.list<RHSignal>('signals', ref => ref).valueChanges().subscribe(res => {
this.listSignal = res;
})
}
删除数组的函数
ClearSignals(){
this.db.object('signals').remove();
alert('Signals has been removed !!!');
}
如您所见,"signal" 节点(可以包含 200 多个结构化项目)被一个命令删除,但绑定继续发送每个删除的数组项目的数据。
这会导致页面变得非常慢或被阻塞。
我需要一个更快更好的方法来避免这个问题。
感谢支持
终于找到了解决办法:
通过这个阅读官方文档后link:
https://firebase.google.com/docs/database/web/read-and-write
我发现:
Delete data
The simplest way to delete data is to call remove() on a reference to the location of that data.
You can also delete by specifying null as the value for another write operation such as set() or update(). You can use this technique with update() to delete multiple children in a single API call.
所以我把清除方法改成这样:
ClearSignals(){
var updates = {};
updates['/signals'] = {};
this.db.database.ref().update(updates);
alert('Signals has been removed !!!');
}
现在速度真快!