Firestore 断开连接

Firestore disconnect

断网后触发监听器"no internet"。连接恢复后,"no internet" 功能也起作用了。 success 不行,只能重启应用后。如何修复?

根据 official documentation 关于 Cloud Firestore 中事务的使用:

Transactions will fail when the client is offline.

换句话说,您无法在离线状态下使用交易。

另请记住,当网络连接中断时(用户设备上没有网络连接),onSuccess()onFailure() 方法都不会被触发。这种行为是有道理的,因为只有当数据被 Firebase 服务器提交(或拒绝)时,任务才被视为完成。

如果您想检查您是从服务器还是缓存接收数据,请在快照事件中使用 SnapshotMetadata 上的 fromCache 属性。如果 fromCachetrue,数据来自缓存,可能不完整。如果 fromCachefalse,数据是完整的并且是服务器上的最新更新。