'Enabling offline for firebase realtime database using Ionic app with capacitor
I am using ionic with firebase realtime database and capacitor 3. I intend to enable offline capabilities. I have built the app using ionic cap build and then opened in xcode. Then following url https://firebase.google.com/docs/database/ios/offline-capabilities I added the below code in AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
Database.database().isPersistenceEnabled = true
return true
}
Now to test i ran the app with wifi on and got the data from firebase db. after this i killed the app and turned off wifi. However, on launching the app it does not load the data.
Is there anything else i am missing here?
my key pod file has:
target 'App' do
capacitor_pods
# Add your Pods here
pod 'FirebaseCore', '7.11.0' # Add this line
pod 'Firebase/Database', '7.11.0' # Add this line
end
Below is my code that does not work and expected to:
getSeedConfig(){
return new Promise((resolve, reject) =>{
const doc = ref(this.db, 'config/seed');
get(doc).then((snapshot) => {
if (snapshot.exists()) {
resolve(snapshot.val())
} else {
resolve(null)
}
}).catch((error) => {
reject(error)
});
})
}
Solution 1:[1]
When using a modern API in JavaScript there are not many cases where you need to return a custom promise anymore.
As far as I can tell this code that you now have:
getSeedConfig(){
return new Promise((resolve, reject) =>{
const doc = ref(this.db, 'config/seed');
get(doc).then((snapshot) => {
if (snapshot.exists()) {
resolve(snapshot.val())
} else {
resolve(null)
}
}).catch((error) => {
reject(error)
});
})
}
Can be shortened to:
getSeedConfig(){
const doc = ref(this.db, 'config/seed');
return get(doc).then((snapshot) => {
return snapshot.val(); // ? returns null when the snapshot does not exist
})
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
