javascript - 为什么 firestore / AngularFire SDK 会为一个请求打包多个 Promise?
问题描述
这是我编写文档及其子集合的方式:
public async setEvent(event: EventInterface): Promise<void[]> {
return new Promise<void[]>(async (resolve, reject) => {
const writePromises: Promise<void>[] = [];
event.setID(event.getID() || this.afs.createId());
event.getActivities()
.forEach((activity) => {
activity.setID(activity.getID() || this.afs.createId());
writePromises.push(this.afs.collection('events').doc(event.getID()).collection('activities').doc(activity.getID()).set(activity.toJSON()));
activity.getAllStreams().forEach((stream) => {
this.logger.info(`Steam ${stream.type} has size of GZIP ${getSize(this.getBlobFromStreamData(stream.data))}`);
writePromises.push(this.afs
.collection('events')
.doc(event.getID())
.collection('activities')
.doc(activity.getID())
.collection('streams')
.doc(stream.type) // @todo check this how it behaves
.set({
type: stream.type,
data: this.getBlobFromStreamData(stream.data),
}))
});
});
try {
await Promise.all(writePromises);
await this.afs.collection('events').doc(event.getID()).set(event.toJSON());
resolve()
} catch (e) {
Raven.captureException(e);
// Try to delete the parent entity and all subdata
await this.deleteEvent(event.getID());
reject('Something went wrong')
}
})
}
但是,当我查看网络选项卡时:
我看到一个请求启动了,到目前为止还不错,req_0
数据是我的activity
,但在同一个请求上进一步看,我可以看到:
所以它增加了更多的数据,这不应该发生,因为:a)我将请求的大小传递给 firestore(1mb)b)由于连接速度慢我超过了写入的时间限制。
最有趣的是,当我的网络速度较慢时会发生这种行为。
编辑:这是请求示例的有效负载:
任何人,来解释这是为什么?
解决方案
发生的是所谓的批处理,因此您的写入操作不会立即触发,它们将聚合为单个请求,因为执行网络 I/O 在时间和电池寿命方面是昂贵的。
最小化网络 I/O 可以节省电池寿命(如上所述),这实际上是主要问题。
引擎盖下发生了“魔术”
推荐阅读
- azure-devops - 获取 Azure DevOps Pipeline Artifact 输出 Windows 应用程序
- c++ - 具有依赖字段的结构的聚合初始化
- javascript - 如何通过增加输入数字来动态更改价格
- ios - 应用内购买服务器验证是强制性的吗?
- python - Python - 解码的 unicode 字符串不会保持解码状态
- html - Ruby on Rails 我想在按下“submit_tag”时在另一个选项卡上输出“file_field_tag”选择的 csv 文件的内容
- docker - docker-compose 问题:尝试创建/挂载卷时权限被拒绝
- c++ - 在递归函数中使用字符串的 size() 函数会导致较大的延迟/复杂性吗?
- python - 获取 mysql DB 列以列出
- python - 为什么 Matplotlib+Basemap 不显示岛屿?