[英]Can you authenticate, then make multiple dependent requests for data using Cloud Firestore?
In my app, a user can log in, then a list of their "pages" (a collection of documents) is retrieved, and their "selectedPageId" is retrieved.在我的应用程序中,用户可以登录,然后检索他们的“页面”(文档集合)列表,并检索他们的“selectedPageId”。 This way the UI can show a list of page names, and the content of the selected page.
这样,UI 可以显示页面名称列表以及所选页面的内容。
This means three requests have to happen, each waiting for the other:这意味着必须发生三个请求,每个请求都在等待另一个:
Is there a way to make all this happen on the server at once so I can make a single request, which I assume would make my UI render more quickly?有没有办法让这一切立即在服务器上发生,这样我就可以发出一个请求,我认为这会使我的 UI 渲染得更快?
As Dharmaraj mentioned in his comment, "Firebase Auth will still be required on client side".正如 Dharmaraj 在他的评论中提到的那样,“客户端仍然需要 Firebase 身份验证”。 However , is you use the default Auth state persistence (ie
firebase.auth.Auth.Persistence.LOCAL
) once a user is signed in he/she doesn't need to sign in the next times ("the state will be persisted even when the browser window is closed. An explicit sign out is needed to clear that state"). However , is you use the default Auth state persistence (ie
firebase.auth.Auth.Persistence.LOCAL
) once a user is signed in he/she doesn't need to sign in the next times ("the state will be persisted even when浏览器 window 已关闭。需要显式注销才能清除该状态“)。 So signing in is not really a problem IMO.因此,登录实际上并不是 IMO 的问题。
For the Firestore queries: Executing the two queries back-to-back with the second one returning only one document should not take a lot of extra time compare to executing only the first query (which fetches the pages list).对于 Firestore 查询:与仅执行第一个查询(获取页面列表)相比,与仅返回一个文档的第二个查询背靠背执行两个查询不应该花费很多额外的时间。
Having said that, one possible approach would be to save, for each user, a Firestore document containing the list of the user's pages plus the user's selectedPageId
.话虽如此,一种可能的方法是为每个用户保存一个 Firestore文档,其中包含用户的页面列表以及用户的
selectedPageId
。 You can maintain this page with a Cloud Function which, for example, mirrors the list (documents in a collection) with an array in this Firebase doc.您可以使用 Cloud Function 维护此页面,例如,使用此 Firebase 文档中的数组镜像列表(集合中的文档)。 However, you need to take care to not reach the maximum size for a document ie 1MiB (hence my question on the number of fields displayed for a page).
但是,您需要注意不要达到文档的最大大小,即 1MiB(因此我对页面显示的字段数提出了疑问)。
One possible variation in this case would be to initially present, in your first app page, a limited number (eg 25) of pages of the list, with a "Show more" button.在这种情况下,一种可能的变化是最初在您的第一个应用程序页面中显示有限数量(例如 25)的列表页面,并带有“显示更多”按钮。
Finally, note that using a Cloud Function as mentioned in the above comments will very most probably increase the response time compare to standard queries executed via the SDK.最后,请注意,与通过 SDK 执行的标准查询相比,使用上述评论中提到的 Cloud Function 很可能会增加响应时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.