Firestore同步到底能做什么? [英] What exactly does Firestore synchronization do?

查看:60
本文介绍了Firestore同步到底能做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://firebase.google.com/docs/firestore 说:

与实时数据库一样,Cloud Firestore使用数据同步来更新任何已连接设备上的数据.

Like Realtime Database, Cloud Firestore uses data synchronization to update data on any connected device.

我试图弄清楚这种数据同步"由什么组成.

I'm trying to figure out what this "data synchronization" consists of.

https://firebase.google.com/docs/firestore/manage-data/enable-offline 说:

启用持久性后,Cloud Firestore会缓存从后端收到的每个文档以供脱机访问.

When persistence is enabled, Cloud Firestore caches every document received from the backend for offline access.

只要后端发生更改,Cloud Firestore是否会更新该缓存?例如,如果我的缓存中有某个文档,该文档是否在后端更改该文档时自动更新(a)或(b)每当我进行使用该文档的查询时,我碰巧都在线当时?

Does Cloud Firestore update that cache whenever changes happen in the back end? For example, if I have a certain document in my cache, does the document update (a) automatically when that document is changed in the back end or (b) whenever I do a query that uses that document, and I happen to be online at the time?

更新

我要重申我的最后一段,因为它被误解了至少一个人.

I'm going to restate my last paragraph, because it's been misunderstood by at least one person.

想象一下以下情况:

  • 我有一部智能手机.为了使事情变得简单,假设使用智能手机一直在线.
  • 此智能手机上安装了一个包含Firestore SDK的移动应用.
  • 该应用已启用Firestore缓存.
  • 该缓存包含1000个文档.

现在我的问题是:这些文件之一是否在云中更改(不是在智能手机),Firestore SDK在什么情况下会更新文件在缓存中?它会(a)尽快自动更新文件吗在云端更改之后,或(b)下次更新文档时,移动应用程序会查询使用该文档的信息吗?

Now my question is: if one of these documents changes in the cloud (not on the smartphone), under what circumstances will the Firestore SDK update the document in the cache? Will it (a) update the document automatically, soon after the change on the cloud, or (b) update the document the next time the mobile app does a query that uses the document?

推荐答案

我认为突出显示的两个段落都是简单明了的解释,并回答了您的问题:

Both of the highlighted paragraphs are in my opinion straightforward explanations, and to answer your questions:

只要后端发生更改,Firestore都会更新该缓存吗?

Does Firestore update that cache whenever changes happen in the back end?

是的,如果您处于脱机状态并且某些文档已更改,则重新联机后,您将获得所有更改的文档,反之亦然;如果您在本地(在缓存中)更改某些文档,则当您重新联机时,所有更改后的文档将发送到Firebase服务器.

Yes, if you are offline and some documents are changed, once you are back online, you get all changed documents and vice versa, if you change some documents locally (in your cache), when you're back online, all changed documents are sent to the Firebase servers.

如果您有一个查询返回10个文档,则意味着您与10个文档保持同步,这基本上意味着如果这10个文档中的文档被更改,则仅针对该更改触发您的侦听器.

If you have a query that returns 10 documents, it means that you are in sync with 10 documents, which basically means that if of those 10 documents is changed, your listener is triggered only for that change.

在后端更改文档时自动进行文档更新(a)

does the document update (a) automatically when that document is changed in the back end

是的,首先,在您的缓存中,当您重新联机后,也在Firebase服务器上.

Yes, first of all in your cache and once you are back online, also on Firebase servers.

何时我查询使用该文档的查询,而我当时恰好处于在线状态?

whenever I do a query that uses that document, and I happen to be online at the time?

无论何时在线和文档更改,都会实时收到通知.

Whenever you are online and a document is changed, you are notified in realtime.

如果这些文档之一在云中(而不是在智能手机上)更改,那么Firestore SDK在什么情况下会更新缓存中的文档?它会(a)在云上进行更改后不久自动更新文档,还是(b)下次移动应用程序使用该文档进行查询时更新文档?

if one of these documents changes in the cloud (not on the smartphone), under what circumstances will the Firestore SDK update the document in the cache? Will it (a) update the document automatically, soon after the change on the cloud, or (b) update the document the next time the mobile app does a query that uses the document?

只要您与这些文档保持同步,它就会在文档更改的第二时刻发生.同步是指您在代码中使用 get()调用来获取这些文档,或者您正在实时侦听文档更改.只需创建对文档或查询的引用,而无需使用侦听器,则不会以任何方式通知您.

As long as you are in sync with those documents, it happens in the second moment the document is changed. What I mean through in sync is, that you are using in your code a get() call to get those documents or if you are listening in realtime for documents changes. Simply creating a reference to a document or a query without using a listener, you aren't notified in any way.

下次移动应用程序使用文档进行查询时更新文档吗?

update the document the next time the mobile app does a query that uses the document?

由于您在线且与文档同步,因此不正确,会立即触发侦听器.

Is not correct since you are online and in sync with the documents, the listener is triggered instantly.

Edit2

简而言之,每当文档发生更改时,就会调用一个侦听器.下一次应用程序执行查询"的句子不正确,因为在创建查询并附加侦听器时,您始终与数据库保持同步,并且在文档更改时会立即收到通知.不是,文档在数据库中链接在一起,当我查询(在某个时间点)时,我得到了更改,不,当更改发生时,您总是会收到通知.这就是实时数据库的优点,可以在发生更改时通知它:)

In short, a listener is called everytime a change in a document occurs. "Next time the app does the query" is not correct sentence because when you create a query and attach a listener, you are always in sync with the database and when a document is changed you are immediately notified. It is not like, the documents are chainging in the database and when I query (at some point of time), I get the changes, no, you are always notified as the changes occur. That's the beauty of a realtime database, to be notified as the changes are happening :)

这篇关于Firestore同步到底能做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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