Ios 获取Firebase中每条消息的配置文件图像的最佳方法

Ios 获取Firebase中每条消息的配置文件图像的最佳方法,ios,swift,firebase,firebase-realtime-database,chat,Ios,Swift,Firebase,Firebase Realtime Database,Chat,我有以下情况,我聊天并使用Firebase作为后端,我想找到下一个问题的最佳解决方案。如果群组聊天处于打开状态,则每封传入的邮件都应具有发件人配置文件图像。聊天室分为三种结构,即对话、用户对话和消息模型。消息模型只包含senderID,因为我发现不建议存储profileImageURL,因为用户可以更改化身。我想到的第二个选择是在对话模型中保存profileImageURL,当用户使用云功能更改化身以更改它时,这将起作用,但这是一个非常糟糕的决定,因为资源成本(例如,如果用户有300次对话,他将

我有以下情况,我聊天并使用Firebase作为后端,我想找到下一个问题的最佳解决方案。如果群组聊天处于打开状态,则每封传入的邮件都应具有发件人配置文件图像。聊天室分为三种结构,即对话、用户对话和消息模型。消息模型只包含senderID,因为我发现不建议存储profileImageURL,因为用户可以更改化身。我想到的第二个选择是在对话模型中保存profileImageURL,当用户使用云功能更改化身以更改它时,这将起作用,但这是一个非常糟糕的决定,因为资源成本(例如,如果用户有300次对话,他将每天更改化身)。请告诉我,在这种情况下最好的方法是什么

消息模型

 "-KmfKFxY2BsLjpGixowG" : {
        "conversationID" : "-KmfK4m1t2nDKFX_MZr8",
        "creationTimeStamp" : 1.497523097283577E9,
        "id" : "-KmfKFxY2BsLjpGixowG",
        "senderID" : "xpyM19QVjJTgrtdntlbcJPkb0jB2",
        "sendingStatusIndex" : 0,
        "textMessage" : "3reds",
        "typeIndex" : 0
      },
会话模型

"-KmfK4m1t2nDKFX_MZr8" : {
        "id" : "-KmfK4m1t2nDKFX_MZr8",
        "lastMessage" : {
          "conversationID" : "-KmfK4m1t2nDKFX_MZr8",
          "creationTimeStamp" : 1.497591480636771E9,
          "id" : "-KmjP72nyEJUX7yQmwYp",
          "senderID" : "AoG6HmxXE8ahAESx98C2UZ0ieAh1",
          "sendingStatusIndex" : 0,
          "textMessage" : "C",
          "typeIndex" : 0
        },
        "typeIndex" : 0,
        "userAcitivities" : [ {
          "removedChatTimeStamp" : 0,
          "userID" : "xpyM19QVjJTgrtdntlbcJPkb0jB2"
        }, {
          "removedChatTimeStamp" : 0,
          "userID" : "AoG6HmxXE8ahAESx98C2UZ0ieAh1"
        } ]
      }
用户会话模型

 "AoG6HmxXE8ahAESx98C2UZ0ieAh1" : {
        "-KmeqR8RYXo-5Pt0gue1" : {
          "friendID" : "QscqImQoCGdAciaVMoRJN35KjEH2",
          "id" : "-KmeqR8RYXo-5Pt0gue1",
          "removedChatTimeStamp" : 0,
          "typeIndex" : 0
        },
更新说明:

你好!!我在聊天!Firebase用作后端。问题是如何在群聊中最好地上传用户图像。当然,消息模型在应用程序中有一个senderID。每个单元格中出现的最糟糕的选项(我不会使用它)是查询最新的url,并使用翠鸟加载和缓存图像。启动应用程序/聊天时的第二个选项是更新或上传聊天室中可用的用户的所有化身,但这里有两个问题。第一个问题是,如果聊天时间为50次,并且每个聊天时间有50个用户,那么一次执行2500个查询也不是一个选项。第二个问题,如果以某种方式避免了大量的请求,那么从这些数据可以制作一个字典并将其传输到一个单元格,然后通过senderID获得翠鸟的实际url,但我认为这很糟糕,而且还可以说是性能问题。最简单的例子就是基于firebase的聊天


也有几种选择,但都不好。你能告诉我怎么做最好吗?或者在何处查找和阅读此“模块”的正确架构。

我解决了此问题,如下所示,打开ChatViewController时,我使用云函数请求指向所有用户图像的链接,并在应用程序中获得现成的字典[userID:userAvatarPath]

const functions = require('firebase-functions');
const admin = require('firebase-admin');

module.exports = functions.https.onRequest((req, res) => {

    const conversationID = req.query.conversationID;
    const currentUserID = req.query.currentUserID;

    console.log("conversationID", conversationID, "currentUserID", currentUserID);

    const conversationUsersRef = admin.database().ref("chat").child("conversationUsers").child(conversationID);
    conversationUsersRef.once("value").then(conversationUsersRefSnap => {
        var index = 0;
        var totalIndex = 0;
        var conversationUsersImagesArray = [];
        conversationUsersRefSnap.forEach(function(conversationUserSnap) {
            console.log("conversationUserSnap", conversationUserSnap.val());
            console.log("$index", index);
            const dict = conversationUserSnap.val();
            const conversationUserID = dict["userID"];
            if (conversationUserID != currentUserID) {
                index += 1;
                const userSenderImageQuery = admin.database().ref('userImages').child(conversationUserID).child('userProfileImages').orderByChild('timeStamp').limitToLast(1);
                userSenderImageQuery.once('value', function (snapshot, error) {
                    var imagePath = '';
                    if (error) {
                        index -= 1;
                        if (conversationUsersImagesArray.length == index) {
                            console.log("total", conversationUsersImagesArray);
                            res.status(200).send(conversationUsersImagesArray);
                        };
                    } else {
                        if (snapshot.val()) {
                            const value = snapshot.val();
                            const key = Object.keys(value)[0];
                            const requestJSON = value[key];
                            console.log('senderImageQuery requestJSON', requestJSON);
                            const userImagePath = requestJSON['path'];
                            imagePath = userImagePath;
                            const compressPath = requestJSON["compressPath"];
                            if (compressPath) {
                                imagePath = compressPath;
                            };

                            conversationUsersImagesArray.push({"userID" : conversationUserID, "imagePath" : imagePath, "conversationID" : conversationID});
                            console.log("conversationUsersImages", conversationUsersImagesArray.length, "index", index);

                            if (conversationUsersImagesArray.length == index) {
                                console.log("total", conversationUsersImagesArray);
                                res.status(200).send(conversationUsersImagesArray);
                            };
                        } else {
                            index -= 1;
                            if (conversationUsersImagesArray.length == index) {
                                console.log("total", conversationUsersImagesArray);
                                res.status(200).send(conversationUsersImagesArray);
                            };
                        };
                    };
                });
            };
        });
    });
});