首页 > 解决方案 > 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 而不会出现这个问题。

标签: iosfirebasegoogle-cloud-firestoreionic4

解决方案


推荐阅读