Hon*_*yal 10 javascript firebase google-cloud-firestore react-native-firebase
我最近将我的应用程序从 firebase 更新到了 firestore,但一直停留在离线持久化状态。我正在使用 react-native-firebase 集成 firestore 并禁用其持久性,但在没有互联网的情况下仍然没有收到任何捕获错误。这是我删除数据的代码,但当没有互联网时,catch 永远不会出错。
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
});
})
Run Code Online (Sandbox Code Playgroud)
Dou*_*son 16
Firestore 和实时数据库 SDK 在没有网络连接时不会抛出错误。他们会默默地重试连接,希望设备很快重新获得网络连接。这样做的原因是因为大多数开发人员不希望他们的应用程序仅仅因为用户进入隧道然后退出,或以强制网络重置的方式在移动塔之间切换而显得损坏。
为了有效利用在线数据库,设备大部分时间都应该在线,这也是SDK优化的情况。
小智 7
创建您自己的删除辅助函数,如果没有 Internet 连接,该函数会引发错误。.collection()您可以通过不调用而是传递完整路径来使 API 更简洁d + deviceid。
function onlineOnlyDelete(path) {
if(!deviceApi.hasInternet()) throw 'No Internet, no delete'
return firebase.firestore().doc(path).delete()
}
Run Code Online (Sandbox Code Playgroud)
替换firebase.firestore().collection('d').doc(deviceid)为onlineOnlyDelete(d+deviceid),你应该可以开始了
参考这个线程。我设置了以下检查:
.then(() => {
if (snapshot.empty && snapshot.metadata.fromCache) {
throw new Error('Failed to fetch ideas');
}
})
Run Code Online (Sandbox Code Playgroud)
后来我发现了错误,并将应用程序状态设置为包含错误以及特定于批次的参数startIndex,stopIndex这些参数稍后会很重要。渲染文档时,为BatchError我所称的保留了一种情况,在这种情况下,用户将看到一个Refetch附加了回调的按钮。使用批处理元数据,回调能够重新初始化特定的批处理请求。
| 归档时间: |
|
| 查看次数: |
5724 次 |
| 最近记录: |