问题
I'm currently building an Android app which uses Firebases realtime database as it's backend. I observed, that getting the children of a node the first time takes longer so I assume, that Firebase uses a local database. However the local files seem to be deleted if the app is killed (even with persistence enabled).
I wanted to inspect the local database using Stetho, but I can't find the data in the local databases displayed in chromes developer tools.
Is there a way to view how the local database looks like? And is there a possibility to keep the data even if the app is killed?
回答1:
tl;dr: you likely haven't enabled disk persistence.
What happens on your first read from the database
When your app first connects to Firebase, this happens:
The first time you're retrieving data, Firebase sets up a connection between the client and the server. The time this takes depends on the path between the client and the server, but is usually multiple seconds.
If you use authentication, then this initial "handshake" also includes signing the user in, or refreshing their token.
Then Firebase retrieves the data that you requested. The time this takes is mostly dependent on the amount of data you request and the bandwidth of the client device.
If these steps, step 3 often takes the least amount of time. Yet when you request additional data the Firebase client only has to take step 3, since it maintains its connection and authentication state between calls.
Where Firebase caches data
By default Firebase keeps the data that you're actively listening for in memory. If you attach a second listener for the same data, that listener gets a copy of the data from memory. So the read is instant in that case, since even step 3 from before isn't needed.
When you detach all listener for a specific piece of data, Firebase will flush it from memory immediately. So that means that next time you attach a listener, it will have to re-read the data (step 3 from above).
If you enable disk persistence, the Firebase client will also store a copy of any data it receives to disk. This is a so-called MRU cache, so it only contains recent data, and it persists between restarts of your app. But this only happens if you've enabled persistence.
Where the local disk cache lives
The local disk cache is a regular sqlite database. If you're interested in looking at it for debugging purposes, it (for me at least) lives in /data/data/{applicationId}/{applicationId}_default
. It is a sqlite database. But the format is not documented, so you're on your own figuring out anything inside it.
Also see:
- How firebase persistence stores local data for my Android app
来源:https://stackoverflow.com/questions/46849945/view-local-firebase-database-with-stheto-on-android