I recently updated my app from firebase to firestore but stuck at offline persistance. I am using react-native-firebase to integrate firestore and disable its perisistance but still not getting any catch error when no internet. This is my code to delete data but catch never gets error when no internet not is the promise resolved.
firebase.firestore().collection('d').doc(deviceid).delete().then(function () {
console.log("Device Deleted");
that.setState({
loading: false
});
Toast.show('Device Deleted Succesfully', {
duration: Toast.durations.SHORT,
position: Toast.positions.TOP,
shadow: true,
animation: true,
hideOnPress: true,
delay: 0,
});
}).catch(function (err) {
console.log(err);
that.setState({
loading: false
});
})
When you initialize Cloud Firestore, you can enable or disable offline persistence: For Android and Apple platforms, offline persistence is enabled by default.
By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache.
If either the collection or document does not exist, Cloud Firestore creates it.
Make your own helper function for deletion that throws an error if there is no Internet connection. You can make the API cleaner by not calling .collection()
but rather passing the full path d + deviceid
to it.
function onlineOnlyDelete(path) {
if(!deviceApi.hasInternet()) throw 'No Internet, no delete'
return firebase.firestore().doc(path).delete()
}
replace firebase.firestore().collection('d').doc(deviceid)
with onlineOnlyDelete(d+deviceid)
and you should be good to go
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With