【发布时间】:2020-08-24 19:17:58
【问题描述】:
根据描述here
我希望 snapshot.metadata.fromCache 是 true 当正在收听的文档在与侦听器相同的客户端中被修改时,例如
- 本地
.update(...)立即触发onSnapshot处理程序(并收到一个快照,其中fromCache设置为true) - 数据发送到数据库
- firebase 客户端收到返回消息后什么都不做(不会触发
onSnapshot),因为服务器数据与缓存一致。
所以,当本地更改触发 onSnapshot 时,fromCache 应始终为 true。
然而,这似乎只是前两到三个 onSnapshot 响应的情况,之后fromCache 似乎总是false。
示例测试:
// ... firestore init w/ a test project and with persistence enabled.
const db = firebase.firestore();
db.settings({
ignoreUndefinedProperties:true
})
// Where "_test" is an empty collection with full allowance for read/write
await db.collection("_test").doc("deleteme").set({});
let doc = db.collection("_test").doc("deleteme")
// ?! Expect this to be true but after the first one or two occurrences it is always false.
doc.onSnapshot(s=>{console.log("test snapshot change from cache? ",s.metadata.fromCache)})
let x = 0;
let poke = async ()=>{
doc.update({
n:Math.random()
})
await sleep(3000); // generic custom delay
window.requestAnimationFrame(poke)
};
window.requestAnimationFrame(poke);
编辑:这里的问题是由于与其他问题一样缺少知识:Is Firestore onSnapshot update event due to local client Set?
【问题讨论】:
-
我建议编辑问题,以便更清楚地描述程序随时间的行为,以响应您提供的特定输入。说“关于前两个或三个 onSnapshot 响应”并不是很具体。问题中应该有足够的信息,任何人都可以重现您的具体观察结果。
-
"所以,当 onSnapshot 由本地更改触发时,
fromCache应该始终为真。"我不认为它是这样定义的。客户端是否知道其本地快照是否与服务器同步更重要。您可能会将fromCache与hasPendingWrites混淆。确实正如 Doug 评论的那样:看看你想通过这个测试完成什么会很有用,因为可能有其他方法可以做你想做的事。另请参阅:what is the XY problem?. -
"你可能会混淆 fromCache 和 hasPendingWrites" [facepalm] 就是这样!我不得不说,在符合这两个项目描述的情况下,维恩图中有不少重叠。 “...快照包含尚未提交到后端的本地写入(例如,set() 或 update() 调用)的结果”与“快照是从缓存数据创建的”
-
fromCache的名称确实令人困惑。我将fromCache理解为“可能尚未包含来自服务器的所有数据”,而isPending是“可能包含服务器尚不知道的数据”。 -
@FrankvanPuffelen,如果您提交关于
fromCache和isPending之间区别的注释作为答案,我可以选择它作为正确答案。
标签: javascript google-cloud-firestore