离线缓存数据的 Firestore 定价说明
Firestore pricing clarifications for offline cached data
Firestore 会向我收取对本地缓存数据的读取查询费用,这对我来说似乎很奇怪,但我在 Firestore Pricing document 中找不到任何相反的说明。如果我强制 Firebase 进入离线模式,然后对我的本地缓存数据执行读取,我仍然需要为我检索的每个实体付费吗?
其次,我的应用程序中的离线用户会向单个实体写入许多小更新。我希望更改每次都保留在本地(以防他们退出应用程序),但我只需要最终一致地保存到云中。当用户重新连接到 Internet 并且 Firestore 刷新本地更改时,我会为实体的单个写入请求付费,还是为我在离线时进行的每个 update
调用付费?
Firestore 可能非常适合我的用例,但如果离线读取和写入按与在线读取和写入相同的费率收费,那将不是一个经济实惠的选择。
Cloud Firestore supports offline data persistence. This feature caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline. You can write, read, listen to, and query the cached data. When the device comes back online, Cloud Firestore synchronizes any local changes made by your app to the data stored remotely in Cloud Firestore.
因此,每个使用 Firestore 数据库并将 PersistenceEnabled
设置为 true 的客户端都会维护自己的内部(本地)数据库版本。当数据为inserted/updated时,首先写入这个本地版本的数据库。结果,对数据库的所有写入都添加到 queue
。这意味着一旦您重新联机,存储在那里的所有操作都将在 Firebase 服务器上提交。这也意味着这些操作将被视为独立的操作,而不是一个整体。
但请记住,不要将 Firestore 用作仅供离线使用的数据库。它实际上被设计为一个在线数据库,可以在短到中间断开连接的情况下工作。离线时,它将保留写入操作队列。随着此队列的增长,本地操作和应用程序启动将变慢。没什么大不了的,但随着时间的推移,这些可能会加起来。
如果 Google Cloud Firestore
定价模型不适合您的用例,请使用 Firebase Realtime Database
。正如来自 Firebase 官方博客的 post 中提到的,您仍然可能想要使用实时数据库的原因之一是:
As we noted above, Cloud Firestore's pricing model means that applications that perform very large numbers of small reads and writes per second per client could be significantly more expensive than a similarly performing app in the Realtime Database.
所以选择哪个选项取决于您。
根据 this 如果您想使用 Cloud Firestore 完全离线工作,您可以通过以下方式禁用网络:
FirebaseFirestore.getInstance().disableNetwork()
但 firestore 会导致第一个用户获取请求的客户端离线错误,您必须将此错误视为空响应。
Firestore 会向我收取对本地缓存数据的读取查询费用,这对我来说似乎很奇怪,但我在 Firestore Pricing document 中找不到任何相反的说明。如果我强制 Firebase 进入离线模式,然后对我的本地缓存数据执行读取,我仍然需要为我检索的每个实体付费吗?
其次,我的应用程序中的离线用户会向单个实体写入许多小更新。我希望更改每次都保留在本地(以防他们退出应用程序),但我只需要最终一致地保存到云中。当用户重新连接到 Internet 并且 Firestore 刷新本地更改时,我会为实体的单个写入请求付费,还是为我在离线时进行的每个 update
调用付费?
Firestore 可能非常适合我的用例,但如果离线读取和写入按与在线读取和写入相同的费率收费,那将不是一个经济实惠的选择。
Cloud Firestore supports offline data persistence. This feature caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline. You can write, read, listen to, and query the cached data. When the device comes back online, Cloud Firestore synchronizes any local changes made by your app to the data stored remotely in Cloud Firestore.
因此,每个使用 Firestore 数据库并将 PersistenceEnabled
设置为 true 的客户端都会维护自己的内部(本地)数据库版本。当数据为inserted/updated时,首先写入这个本地版本的数据库。结果,对数据库的所有写入都添加到 queue
。这意味着一旦您重新联机,存储在那里的所有操作都将在 Firebase 服务器上提交。这也意味着这些操作将被视为独立的操作,而不是一个整体。
但请记住,不要将 Firestore 用作仅供离线使用的数据库。它实际上被设计为一个在线数据库,可以在短到中间断开连接的情况下工作。离线时,它将保留写入操作队列。随着此队列的增长,本地操作和应用程序启动将变慢。没什么大不了的,但随着时间的推移,这些可能会加起来。
如果 Google Cloud Firestore
定价模型不适合您的用例,请使用 Firebase Realtime Database
。正如来自 Firebase 官方博客的 post 中提到的,您仍然可能想要使用实时数据库的原因之一是:
As we noted above, Cloud Firestore's pricing model means that applications that perform very large numbers of small reads and writes per second per client could be significantly more expensive than a similarly performing app in the Realtime Database.
所以选择哪个选项取决于您。
根据 this 如果您想使用 Cloud Firestore 完全离线工作,您可以通过以下方式禁用网络:
FirebaseFirestore.getInstance().disableNetwork()
但 firestore 会导致第一个用户获取请求的客户端离线错误,您必须将此错误视为空响应。