我在我的应用中使用 Firestore。 According to the documentation ,它支持离线数据持久化,当网络再次打开时,Firestore 同步更改。
这是我的代码。
FirebaseFirestore db = FirebaseFirestore.getInstance();
User user = new User();
db.collection("users").document("doc").set(user);
它按预期工作:如果网络关闭,它会在本地缓存,并在网络再次打开时进行同步。但我不断收到此警告:
W/ManagedChannelImpl:[{0}] 无法解析名称。 status={1}
This post说这是由于没有互联网连接。如果 Firestore 支持离线持久化,为什么我会收到此警告?如何摆脱这个警告?
最佳答案
It works as expected: it cashes locally if the network is off and synchronizes when the network is on again.
这是预期的行为。
But I keep getting this warning:
W/ManagedChannelImpl: [{0}] Failed to resolve name. status={1}
This post says it is due to no internet connection.
发帖没错,是因为没有网络连接。
If Firestore supports offline persistence, why do I get this warning?
你明白了,因为在你重新获得互联网连接的时间和监听器实际激活的时间之间,有一段时间你的客户端没有连接到服务器。这就是为什么除非网络打开并且客户端再次与服务器同步,否则该警告会多次打印而不会停止的原因。
重试没有您预期的那么快的原因是因为执行重试的代码使用了所谓的 exponential backoff。算法。这意味着此代码可以防止所有可能在用户设备上如此快速地发生的重试,以提高性能。重试次数过多也会影响用户,因为他会消耗过多的数据计划带宽。
当您正在监听 Cloud Firestore 数据库中的更改并且您有一些网络断开连接时,这就是正在发生的事情,不幸的是您无能为力。您无法控制 Firebase Firestore SDK 如何管理其连接。
How to get rid off this warning?
IMO,因为它只是一个警告而不是不是异常,并且它只显示几秒钟直到重新建立连接,您可以简单地忽略它。
但如果你真的想摆脱它,下面的代码可能会对你有所帮助。请注意,我还没有测试过。
InstantiatingGrpcChannelProvider channelProvider = InstantiatingGrpcChannelProvider.newBuilder()
.setKeepAliveTime(Duration.ofSeconds(60L))
.setKeepAliveTimeout(Duration.ofMinutes(5L))
.build();
FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder()
.setChannelProvider(channelProvider).build();
FirebaseOptions firebaseOptions = new FirebaseOptions.Builder()
.setCredentials(credentials).setFirestoreOptions(firestoreOptions)
.setConnectTimeout(5000).setReadTimeout(5000).build();
FirebaseApp firebaseApp = FirebaseApp.initializeApp(firebaseOptions);
Firestore firestore = FirestoreClient.getFirestore(firebaseApp);
关于android - Firestore 离线写入数据时的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58303341/