当本地更改时,onSnapshot fromCache始终为false [英] onSnapshot fromCache always false when change is local
问题描述
根据说明此处我希望在与侦听器相同的客户端中修改正在侦听的文档时, true
.
According to the description here
I'd expect snapshot.metadata.fromCache
to be true
when the document being listened to is modified in the same client as the listener, e.g.
- 本地
.update(...)
立即触发onSnapshot
处理程序(并通过将fromCache
设置为true
) - 数据已发送到数据库
- firebase客户端收到返回消息,但不执行任何操作(不会触发
onSnapshot
),因为服务器数据与缓存一致.
- The local
.update(...)
immediately triggers theonSnapshot
handler (and is handed a snapshot withfromCache
set totrue
) - The data is sent to the db
- The firebase client receives the return message and does nothing (does not trigger a
onSnapshot
) because the server data agrees with the cache.
Ergo, fromCache
在本地快照触发onSnapshot时应始终为 true
.
Ergo, fromCache
should always be true
when onSnapshot is trigged by a local change.
但是,仅在前两到三个onSnapshot响应中出现这种情况,此后 fromCache
似乎始终是 false
.
However, this only appears to be the case on the first two to three onSnapshot responses, after-which fromCache
appears to always be 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);
Edit: The question here is due to similarly missing knowledge as in this other question: Is Firestore onSnapshot update event due to local client Set?
推荐答案
因此,当onSnapshot被本地更改触发时,fromCache应该始终为true.
Ergo, fromCache should always be true when onSnapshot is trigged by a local change.
我不认为它是这样定义的;客户端是否知道其本地快照是否与服务器有关是更多.
I don't think that is how it is defined; it is more whether the client knows if its local snapshot is up-to-date with the server.
您可能会将 fromCache
与 hasPendingWrites
混淆. fromCache
名称确实令人困惑.我理解 fromCache
为可能尚未包含来自服务器的所有数据",而 isPending
为可能包含服务器尚不知道的数据".
You maybe confusing fromCache
with hasPendingWrites
. The fromCache
name is indeed confusing. I understand fromCache
as "may not contain all data from the server yet", while the isPending
is "may contain data that the server doesn't know about yet".
这篇关于当本地更改时,onSnapshot fromCache始终为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!