离线时在Firestore中添加/更新数据的正确方法是什么? [英] What's the proper way to add/update data in Firestore when offline?

查看:84
本文介绍了离线时在Firestore中添加/更新数据的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的应用程序离线且正在添加或更新文档时,内存会增加.同样,显示文档列表需要更长的时间来加载.如果我在设备在线时运行相同的代码,则内存将保持一致,并且具有文档列表的活动的速度也将保持一致.

When my app is offline and I am adding or updating a document the memory increases. Also, showing lists of documents take longer to load. If I run the same code when the device is online the memory stays consistent as well as the speed of the activities that have lists of documents.

我目前正在执行以下保存操作:

I'm currently doing saves like the following:

collectionRef.document(id).set(obj, SetOptions.merge());

或者用于批处理几条记录:

Or for batching a couple of records:

batch.set(docRef1, obj1);
batch.set(docRef2, obj2);
batch.commit();

我有onComplete的侦听器,但在的可接受答案中这个问题,似乎表明在大多数情况下都不需要侦听器,并且即使在离线状态下也无法等待侦听器完成.

I had listeners for onComplete but in the accepted answer for this question, it seems to indicate that listeners are unnecessary in most situations and that you can't wait for it to complete anyway when you're offline.

在另一个问题中,它们在代码中指示正确进行联机和脱机保存需要快照":

In another question they indicate in code that a "Snapshot" is required to properly do online and offline saving: Offline issue with Firestore vs Firebase. But I can't find anywhere else indicating if that will make a difference. I think of Snapshots as being something you attach to a document or query when you want to be notified of changes to it and attaching a listener like that will result in a memory leak if it isn't removed.

所有这些的另一部分是这种缓慢可能如何影响数据完整性.当我在Android Studio中的事件探查器中观看时,我看到FirestoreWorker可以一直工作,即使我在应用程序中不做任何事情.我不只是说几秒钟,更像是一分钟.我无法找到离线状态下的任何写入顺序保证.尝试停止并重新启动应用程序似乎不会对速度产生任何影响(尽管它将重置内存).

Another part of all of this is how this slowness might affect data integrity. When I watch in the profiler in Android Studio I see that the FirestoreWorker can get to a point where it is constantly working even if I don't do anything in the app. I'm not just talking a few seconds, more like a minute. There isn't any ordering guarantee of writes when it is offline that I can find. Trying to stop and restart the app doesn't seem to have any effect on the slowness (although it will reset the memory).

所有这些都引出了一个问题:离线时在Firestore中添加/更新数据的正确方法是什么,以使应用程序的内存不会无限制地增长和变慢?

So all of this leads to the question: what is the proper way to add/update data in Firestore when offline so that the app's memory doesn't grow unbounded and slow down?

推荐答案

Cloud Firestore使用SQLite作为其持久性机制.因此,在间歇性的离线活动期间,您应该不会遇到性能或耐用性方面的问题.

Cloud Firestore uses SQLite for its persistence mechanism. So for intermittent periods of offline activity, you shouldn't have problems with performance or durability.

但是,如果您打算长时间使用Firestore数据库,则应注意一些事项. Cloud Firestore并非构建为脱机数据库,而是一种在线数据库,当您在shortlonger时段内处于脱机状态时,它仍可继续工作.脱机时,尚未同步到服务器的暂挂写入将保留在队列中.如果您进行过多的写入操作而没有在线同步它们,则该队列将快速增长,并且不会仅降低write操作的速度,也会降低您的read操作的速度.

However, if you intend to use a Firestore database for very long periods of time, there are some things you should be aware of. Cloud Firestore was not built as an offline database, is an online database that continues to work when you're offline for short or longer periods of time. When offline, pending writes that have not yet been synced to the server are held in a queue. If you do too many write operations without going online to sync them, that queue will grow fast and it will not slow down only the write operations it will also slow down your read operations.

因此,我建议将此数据库用于其在线功能.正如一位Firebase工程师所说,我引用在Firestore中建立缓慢的查询是不可能的".因此,性能来自后端的新索引功能,而当您离线时,这些优化就不存在了.

So I suggest use this database for its online capabilities. As one of the Firebase engineers said and I quote, "It is impossible to build a slow query in Firestore". So, the performance comes from the new indexing capabilities on the backend and these optimizations don't exist when you're offline.

还有一件事,如果您有许多试图写入同一文档的脱机客户端,则状态更改后,实际上只有最后一个会被写入服务器.

One more thing, if you have many offline clients who are trying to write to the same document, only the last one will be actually be written to servers when the state is changed.

因此,要回答您的问题,没有适当的方法可以在脱机时在Firestore中添加/更新数据,以减少内存使用量.只是上网就可以了!

So, to answer your question, there is no proper way to add/update data in Firestore when offline, to have a less memory usage. Just go online and that's it!

这篇关于离线时在Firestore中添加/更新数据的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆