Firebase:第一次写入很慢 [英] Firebase: First write is slow
问题描述
当前正在开发使用离子技术的混合移动应用程序.当应用启动时,并且用户首次写入实时数据库,它总是会延迟大约10秒钟或更长时间.但是随后的所有写入几乎都是瞬时的(不到1秒).
Currently developing a hybrid mobile app using ionic. When the app starts up, and a user writes to the Realtime Database for the first time, it's always delayed by around 10 or more seconds. But any subsequent writes are almost instantaneous (less than 1 second).
我的延迟计算基于在Firebase控制台中查看数据库.
My calculation of delay is based on watching the database in the Firebase console.
这是一个已知问题,还是我做错了什么.请分享您的看法.
Is this a known issue, or maybe I am doing something wrong. Please share your views.
写入操作是通过Firebase Cloud Function进行的.
The write is happening via Firebase Cloud Function.
这是对Firebase Cloud函数的调用
This is the call to the Firebase Cloud function
this.http.post(url+"/favouritesAndNotes", obj, this.httpOptions)
.subscribe((data) => {
console.log(data);
},(error)=>{
console.log(error);
});
这是实际功能
app.post('/favouritesAndNotes', (request, response) => {
var db = admin.database().ref("users/" + request.body.uid);
var favourites = request.body.favourites;
var notes = request.body.notes;
if(favourites!==undefined){
db.child("favourites/").set(favourites);
}
if(notes!==undefined){
db.child("notes/").set(notes);
}
console.log("Write successfull");
response.status(200).end();
});
推荐答案
首次在客户端实例中与Firebase数据库进行交互时,客户端/SDK必须做一些事情:
The first time you interact with the Firebase Database in a client instance, the client/SDK has to do quite some things:
- 如果您使用的是身份验证,则需要检查其所拥有的令牌是否仍然有效,如果没有,请刷新它.
- 它需要找到数据库当前所在的服务器.
- 它需要建立一个Web套接字连接.
其中的每一个都可能需要多次往返,因此,即使您离服务器只有几百毫秒的路程,它的总和也会增加.
Each of these may take multiple round trips, so even if you're a few hundred ms from the servers, it adds up.
来自同一客户端的后续操作不必执行这些步骤,因此会更快.
Subsequent operations from the same client don't have to perform these steps, so are going to be much faster.
如果您想查看实际情况,建议您查看浏览器的网络"选项卡.对于实时数据库,我建议检查网络"选项卡的"WS/Web套接字"面板,您可以在其中查看实际的数据帧.
If you want to see what's actually happening, I recommend checking the Network tab of your browser. For the realtime database specifically, I recommend checking the WS/Web Socket panel of the Network tab, where you can see the actual data frames.
这篇关于Firebase:第一次写入很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!