Reputation: 382
I am using update()
to set the post likes and likes count. Currently, update()
persists the data when offline.
What I want - When the user is offline, they should not able to like the post. An appropriate network error message should be shown instead.
Problem - update()
is not throwing network exception and commits data to the server whenever the user gets online.
Below is what I am doing currently:
db.child(nodepath).update(fanoutObj).then((){
console.log("Data saved successfully.");
}).catch(function(error) {
console.log("Data could not be saved." + error);
});
catch
is never called in case of no internet.
Questions:
Can I disable firebase offline mode and have it throw network exception on update()
when there is no internet connection?
Or should I check for network connection separately on button click?
Upvotes: 2
Views: 741
Reputation: 317477
Realtime Database (and Firestore) make the assumption that clients are generally connected. Lack of connectivity is considered a temporary issue. Therefore, the SDKs will silently retry connections and will never generate errors due to lack of connectivity. There is nothing you can do to change this behavior.
Instead, you could use Cloud Functions to create an HTTP endpoint to call that performs the database work. Your HTTP client will then generate errors if it's unable to connect.
Upvotes: 5