当更改是本地的时,onSnapshot fromCache 始终为 false [英] onSnapshot fromCache always false when change is local
问题描述
根据描述这里我希望 snapshot.metadata.fromCache
为 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.
因此,当 onSnapshot 由本地更改触发时,fromCache
应始终为 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);
编辑:这里的问题是由于与另一个问题类似的知识缺失:Firestore onSnapshot 更新事件是否由于本地客户端设置?
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 应始终为真.
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屋!