我遇到了一些独特的用例,在这些用例中,以下功能将非常有用。
本质上,我有监听document
中特定更改的组件,我的安全规则是以允许读取的方式设置的,但所有写入都被禁用,因为数据库更新只能通过云函数,因此我正在研究文档,以找出是否可以做这样的事情。
/**
* This update should only happen locally / offline in order to update state of components
* that are listening to changes in this document
*/
myDocumentRef.update({ name: 'newname' });
/**
* Then cloud function is called that performs validation and if correct
* updates document with new data (same as one we set offline above).
* Once that is done listening components will receive new data. If unsuccessful
* we would set document to old data (again offline) and show error.
*/
await myCloudFunction();
因此,我的问题是:是否可以在本地/脱机执行更新(且仅执行更新)?这基本上是使用firebase作为全局存储的“乐观更新”流程。
发布于 2019-11-13 15:54:51
Firestore不提供“仅本地”更新的概念。所有写入将在最方便的情况下与服务器同步。如果写入最终失败,API调用返回的promise (如果仍在内存中)将被拒绝。如果同步发生在应用程序重新启动后,承诺将丢失,并且您无法知道写入是否成功。最终失败的本地写入将在本地缓存中恢复。
相反,您需要做的是以本地侦听器可以告知写入阶段的方式写入数据,可能是通过将元数据写入文档以指示该阶段,并与Cloud函数合作保持更新。
https://stackoverflow.com/questions/58840642
复制相似问题