我一直使用onSnapshot
成功来提醒我的代码有关基础数据的更改,例如
// Set up to listen for changes to the "figures" collection, that is,
// someone has created a new figure that we will want to list on the screen.
setFiguresListener: function () {
// `figuresCR` is a collection reference defined elsewhere
return this.figuresCR.onSnapshot((iFigs) => {
iFigs.forEach((fSnap) => {
const aFigure = figureConverter.fromFirestore(fSnap, null);
const dbid = aFigure.guts.dbid; // ID of the "figure" in the database
nos2.theFigures[dbid] = aFigure; // update the local copy of the data
});
nos2.ui.update();
console.log(` Listener gets ${iFigs.size} figures`);
});
[
on()
功能]侦听特定位置的数据更改。
这是从数据库读取数据的主要方法。初始数据将触发您的回调,并且每当数据更改时都会触发一次回调。使用
off( )
即可停止接收更新。有关更多详细信息,请参见在Web上检索数据。
语法略有不同,并且on()
似乎与相同onSnapshot()
。
那么真正的区别是什么?我们应该使用on()
代替onSnapshot()
吗?
on()
是用于从Firebase实时数据库读取的操作。那是一个与Firestore具有完全不同API的完全不同的数据库。它们基本上没有重叠。on()
Firestore无法进行任何操作。
如果您使用的是Firestore,请忽略有关实时数据库的所有文档,并坚持使用onSnapshot()
获取实时更新。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句