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.
- 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
should always be true
when onSnapshot is trigged by a local change.
However, this only appears to be the case on the first two to three onSnapshot responses, after-which fromCache
appears to always be false
.
Example Test:
// ... 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?