ios - Firebase Firestore 上传方法比其他方法花费的时间要长得多
问题描述
所以我创建了一个使用 Ionic 和 Firebase 作为后端的应用程序。当应用程序在网络浏览器或 iOS 模拟器上运行时,响应速度非常快,应用程序运行良好。然而,在 iOS 上,将任何内容上传到 Firebase 需要很长时间。请注意,从 Firebase 下载信息非常快速且简单。然而,上传会带来问题。我正在测试的 wifi 非常快。有谁知道为什么会这样?
该应用程序最近发布,这对我的很多用户和我自己来说都是一个问题!
更新:因此,经过更多测试后,问题似乎与某些功能有关。这些方法是 .update() 和 .add()
每当我尝试更新 Firebase 中的字段时,都需要很长时间。每当我尝试将文档添加到集合中时,它也需要很长时间。为什么会出现这种情况?这是一些需要永远实现的代码:
async createDMChat(otherUID: string, otherName: string) {
let newDoc: DocumentReference = this.db.firestore.collection('dmchats').doc();
let docId: string = newDoc.id;
let chatsArray = this.dmChats.value;
let timestamp = Date.now();
chatsArray.push(docId);
//Adds to your dm chat
await this.db.collection('users').doc('dmchatinfo').collection('dmchatinfo').doc(this.dataService.uid.value).set({
chatsArray: chatsArray
});
//Adds to other person DM chat
//-------------------THIS IS THE PART THAT TAKES FOREVER-----------------------
//The .update() method is the problem as well as .add() to a collection
await this.db.collection('users').doc('dmchatinfo').collection('dmchatinfo').doc(otherUID).update({
chatsArray: firebase.firestore.FieldValue.arrayUnion(docId)
});
//Pull info on person's UID
let otherUserInfo = await this.db.firestore.collection('users').doc('user').collection('user').doc(otherUID).get();
let otherAvatar = otherUserInfo.data().avatar;
//Sets message in database
await newDoc.set({
chatName: otherName + " & " + this.dataService.user.value.name,
users: [otherUID, this.dataService.uid.value],
lastPosted: timestamp,
avatar1: this.dataService.avatarUrl.value,
avatar2: otherAvatar,
person1: otherName,
person2: this.dataService.user.value.name
});
await newDoc.collection('messages').doc('init').set({
init: 'init'
});
await this.dataService.storage.set(docId, timestamp);
}
在上面的代码中, .update() 是永远需要的方法。使用 .add() 方法将文档添加到集合中的其他功能也需要很长时间。
同样,这些方法在网络浏览器和模拟器上速度很快。只是不在移动应用程序中。
==================================================== ==========================
新更新:看来问题实际上在于等待 Promise 返回。我重写了所有以前不再使用 add() 或 update() 的函数,而是在使用 doc() 制作新文档后使用 set() 替换 add()。然后我使用 set({...},{merge: true}) 来替换 update()。
这一次对服务器的更改是即时的,但是在等待方法从服务器返回承诺时出现了问题。这是导致现在滞后的部分。有谁知道为什么会这样?我可以简单地将我的代码更改为不等待这些承诺返回,但我想在我的代码中保留 await 而不会出现这个问题。
解决方案
推荐阅读
- php - PHP 谷歌分析报告 API。检索图表中的数据
- c# - .Net 控制台应用程序 - 将多个 Excel 工作表合并到单个工作表中引发“内存不足”错误
- linux - 以非阻塞进程运行soap-ui mockservicerunner
- vue.js - 使用 bootstrap-vue 将 prop 从父亲传递给子模态组件
- ajax - Laravel 5.7 上的 ajax 后返回 405(不允许的方法)
- powerbi-embedded - PowerBI 嵌入式过滤器不传播到书签
- laravel-5.7 - 如何在我的查看页面中将加密密码显示为解密值?
- html - 如何将框内的文本移动到特定位置
- python - 按下按钮后打开一个新窗口/没有/类 PyQT5
- android - 改造不写所有请求的数据