问题
For a new project we want to experiment with Firebase Database, the idea is to store SEO, Product, User Preferences and so on.
We are expecting that the SEO data will be the bigger space consumer and the client doesn't need to save this information locally as will be only used for statistical purposes
Is it possible to enable Firebase Database Persistence only on certain nodes and exclude others?
Thanks for your help
回答1:
Persistence can only be configured all or nothing. This is true for both Realtime Database and Cloud Firestore. There is no granular persistence setting. Please feel free to file a feature request.
回答2:
To do this you can configure two separate firebase instances - one with persistence turned ON and another one with persistence turned OFF, even if they both point to the same database (or you can configure for several different databases - that exactly what it was created for).
The example below is in Swift, but I suppose there should be equivalents on other platforms:
if let path = Bundle.main.path(forResource: firebasePlistFileName, ofType: "plist"), let options = FirebaseOptions(contentsOfFile: path) {
FirebaseApp.configure(options: options)
FirebaseApp.configure(name: "DatabaseWithoutPersitence", options: options)
}
Database.database().isPersistenceEnabled = true
if let appWithoutPersistence = FirebaseApp.app(name: "DatabaseWithoutPersitence") {
Database.database(app: appWithoutPersistence).isPersistenceEnabled = false
}
When I want to access a database with persistence I address a shared database via Database.database().reference()
and in case I need fresh data I refer to the non-persistent one via Database.database(app: FirebaseApp.app(name: "DatabaseWithoutPersitence")!).reference()
来源:https://stackoverflow.com/questions/54950004/enable-firebase-persistence-on-certain-nodes