gpt4 book ai didi

android - Firestore 重新连接到 Internet 后无法检索数据

转载 作者:IT老高 更新时间:2023-10-28 23:25:19 28 4
gpt4 key购买 nike

我将 Firestore 与 Android SDK (11.6.2) 结合使用,当我的设备离线并重新连接到 Internet 时遇到异常。

请求文档时,firestore 失败并出现以下任务异常:

com.google.firebase.firestore.FirebaseFirestoreException: Failed to get document because the client is offline.

但是,设备已连接,我可以在使用 Firestore 之外发出网络请求,并且它们成功了。这个错误是不一致的,有时重新连接互联网后请求会成功。有时,请求一次又一次地失败,然后成功,有时在设备重新连接到 Internet 后超过一分钟。

这是一个产生异常的示例请求:

val docRef = firestore.collection("foo").document("bar")
docRef.get().addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.d("FirestoreSourceSet", "Get document success")
} else {
Log.e("FirestoreSourceSet", "Get document error", task.exception)
}
}

我没有使用 Firestore 的离线功能,因此 FirebaseFirestore 实例第一次使用 setPersistenceEnabled(false) 标志进行初始化:

val firestoreSettings = FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(false)
.build()

val firestore = FirebaseFirestore.getInstance().apply {
this.firestoreSettings = firestoreSettings
}

为什么即使设备在线,Firestore 仍会返回此错误?我是否在 Firestore 配置中遗漏了可以避免此错误的内容?

我尝试将 Firebase 升级到 11.8.0 版本,但遇到相同的行为。

日志

这些是在离开飞行模式后尝试使用 Firestore 获取同步一些数据(从文档获取开始)时的日志:https://pastebin.com/xDMG2Pzj

在第一次调用 Firestore 之前网络已经可用,因为我等待 Wifi 稳定下来,并使用 ConnectivityManager 进行检查继续使用 Firestore 之前的 Android 版本。

多次调用是因为我每次收到错误时都使用按钮手动重试,直到成功检索到文档。

日志的第一行是当我打开飞行模式时,它关闭了 Firestore 的流。

编辑:问题

Firebase 没有公共(public)跟踪器,但我使用 their report tool 报告了该问题, 和 made a repo that reproduces the issue .

他们承认了这个问题,但无法提供预计到达时间,所以我们必须等待:

If we release the fix, we will announce it in our release notes page.

这仍然是 firestore-core 17.0.4 的问题

最佳答案

来自 official documentation :

To use offline persistence, you don't need to make any changes to the code that you use to access Cloud Firestore data. With offline persistence enabled, the Cloud Firestore client library automatically manages online and offline data access and synchronizes local data when the device is back online.

初始化 Cloud Firestore 时,您可以启用或禁用离线持久性。所以,当使用下面这行代码时:

val firestoreSettings = FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(false)
.build()

您实际上将持久性设置为 false,您正在禁用此功能。要解决这个问题,只需删除这行代码。 Firestore 默认有 .setPersistenceEnabled(true)

关于android - Firestore 重新连接到 Internet 后无法检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48188476/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com