Firebase Database离线读写行为澄清:特定场景疑问
Great question—this is a super common point of confusion when working with Firebase's offline features, so let's break it down clearly:
The Short Answer
When you perform that second offline query to /companies/, you'll get 3 keys (including the new /companies/3333333/ you just saved offline).
Why This Happens
Firebase's offline caching system is built to keep your app's user experience consistent, whether you're online or offline. Here's the play-by-play for your scenario:
- Step 1: Your first offline query to
/companies/pulls data from the local cache, which holds the two existing entries (1111111and2222222). - Step 2: When you save the new company data to
/companies/3333333/while offline, Firebase doesn't just queue the sync for later—it immediately writes this new data to your local cache and marks the operation as pending to send to the cloud once you're back online. - Step 3: Your next query to
/companies/reads directly from this updated local cache, so it includes the newly added entry right away.
This behavior is intentional: Firebase wants users to see the results of their actions instantly, even without an internet connection. It eliminates that frustrating "did that actually save?" uncertainty that can happen with less polished offline apps.
Quick Bonus Note
Just to confirm: Once you reconnect to the internet, that pending write will sync to the Firebase cloud automatically, and other users/devices will see the new company entry as expected.
内容的提问来源于stack exchange,提问作者Daniel Benedykt

