Reputation: 257
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
});
})
Upvotes: 15
Views: 12009
Reputation: 438
You may use a transaction that will actually throw an error if it's unsuccessful. It has timeout and maxAttempts parameters.
Upvotes: 1
Reputation: 4317
Referencing this thread. I set up the following check:
.then(() => {
if (snapshot.empty && snapshot.metadata.fromCache) {
throw new Error('Failed to fetch ideas');
}
})
Then later on I caught the error and set the app state to include the error along with parameters specific to the batch like startIndex
and stopIndex
which will be important later. When rendering the documents, one case is reserved for the BatchError
as I dubbed it, in which case the user is going to see a Refetch
button which has a callback attached to it. Using the batch metadata, the callback is able to reinitialize the specific batch request.
Upvotes: 5
Reputation: 178
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
Upvotes: 7
Reputation: 317467
Firestore and Realtime Database SDKs don't throw errors when there is no network connectivity. They will silently retry the connection in hopes that the device will regain network connectivity soon. The reason for this is because most developers don't want their apps to appear broken just because the user went into a tunnel and back out, or switched between mobile towers in a way that forced a network reset.
In order to effectively make use of an online database, the device should be online most of the time, and this the situation that the SDK is optimizing for.
Upvotes: 22