根据描述在这里我期望snapshot.metadata.fromCache
是true
当文档被听取了相同的客户端修改为听者,如
.update(...)
立即触发onSnapshot
处理程序(并将快照fromCache
设置为true
)onSnapshot
),因为服务器数据与缓存一致。因此,fromCache
应该始终在true
本地更改触发onSnapshot时使用。
但是,仅在前两到三个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);
编辑:这里的问题是由于与其他问题类似的知识缺失:Firestore onSnapshot更新事件是否是由于本地客户端集引起的?
因此,当onSnapshot被本地更改触发时,fromCache应该始终为true。
我不认为这是如何定义的;客户端是否知道其本地快照是否与服务器有关是更多。
您也许混淆fromCache
与hasPendingWrites
。这个fromCache
名字确实令人困惑。我理解fromCache
为“可能尚未包含服务器中的所有数据”,而“isPending
则可能包含服务器尚不知道的数据”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句