JSQMessagesViewController:如何在 viewDidLoad 中获取 senderId?

JSQMessagesViewController: How to get senderId in viewDidLoad?

我有一个在 viewDidLoad 中调用的头像函数(如果它有帮助,这就是函数,尽管我认为问题不在于函数本身:https://pastebin.com/ksHmucTX)。

viewDidLoad中我称它为:

createAvatar(senderId: self.senderId, senderDisplayName: self.senderDisplayName, user: self.currentUser!,  color: UIColor.lightGray)

但是由于我只是传递我的信息(作为当前用户),所以在使用该应用程序时我只能看到我的头像,而看不到聊天中其他人的头像。

avatarImageDataForItemAt函数中,有一个索引路径可以找出哪个消息是哪个:

override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {

let message = messages[indexPath.row]
return avatars[message.senderId]
}

所以我想知道如何在 viewDidLoad 中获得正确的 senderId(即发送消息的人的 ID),以便我可以将其插入 createAvatars 调用,而不是什么我现在有,其中 senderId 只是我的 ID - 从而使每个人的头像都出现,而不仅仅是我的(或当前用户的任何人)。

感谢您的帮助!

编辑:

func getParticipantInfo() {
    let groupRef = databaseRef.child("groups").child(currentRoomIdGlobal)
    groupRef.observe(.childAdded, with: { snapshot in
        if let snapDict = snapshot.value as? [String : AnyObject] {
            for each in snapDict {
                let uid  = each.key
                let avatar = each.value["profilePicture"] as! String
                let gender = each.value["gender"] as! String
                let handle = each.value["handle"] as! String
                let name = each.value["name"] as! String
                let status = each.value["status"] as! String

                // Set those to the dictionaries [UID : value]
                self.avatarDictionary.setValue(avatar, forKey: uid)
                self.nameDictionary.setValue(name, forKey: uid)
                self.genderDictionary.setValue(gender, forKey: uid)
                self.handleDictionary.setValue(handle, forKey: uid)
                self.statusDictionary.setValue(status, forKey: uid)
                DispatchQueue.main.async {
                    self.createAvatar(senderId: uid, senderDisplayName: name, photoUrl: avatar,  color: UIColor.lightGray)
                    self.navCollectionView?.collectionView?.reloadData()
                }
            }
        }
    })
}

创建头像:

func createAvatar(senderId: String, senderDisplayName: String,  photoUrl: String, color: UIColor) {
    if self.avatars[senderId] == nil {
        let img = MyImageCache.sharedCache.object(forKey: senderId as AnyObject) as? UIImage
        if img != nil {
            self.avatars[senderId] = JSQMessagesAvatarImageFactory.avatarImage(with: img, diameter: 30)
        } else if photoUrl != "" {
            // the images are very small, so the following methods work just fine, no need for Alamofire here
            if photoUrl.contains("https://firebasestorage.googleapis.com") {
                self.storage.reference(forURL: photoUrl).data(withMaxSize: 1 * 1024 * 1024) { (data, error) -> Void in
                    if (error != nil) {
                        assertionFailure("Error with Firebase Storage")
                    }
                    else {
                        let newImage = UIImage(data: data!)
                        self.avatars[senderId] = JSQMessagesAvatarImageFactory.avatarImage(with: newImage, diameter: 30)

                        MyImageCache.sharedCache.setObject(newImage!, forKey: senderId as AnyObject, cost: data!.count)
                    }
                }
            } else if let data = NSData(contentsOf: NSURL(string:photoUrl)! as URL) {
                let newImage = UIImage(data: data as Data)!
                self.avatars[senderId] = JSQMessagesAvatarImageFactory.avatarImage(with: newImage, diameter: 30)
                MyImageCache.sharedCache.setObject(newImage, forKey: senderId as AnyObject, cost: data.length)
            } else {
                // Initials
                let senderName = nameDictionary.value(forKey: senderId) as! String
                let initials = senderName.components(separatedBy: " ").reduce("") { ([=13=] == "" ? "" : "\([=13=].characters.first!)") + "\(.characters.first!)" }

                let placeholder = JSQMessagesAvatarImageFactory.avatarImage(withUserInitials: initials, backgroundColor: UIColor.gray, textColor: UIColor.white, font: UIFont.systemFont(ofSize: 14), diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))

                MyImageCache.sharedCache.setObject(placeholder!, forKey: senderId as AnyObject)
            }
        }
    } else {
        // Initials
        let senderName = nameDictionary.value(forKey: senderId) as! String
        let initials = senderName.components(separatedBy: " ").reduce("") { ([=13=] == "" ? "" : "\([=13=].characters.first!)") + "\(.characters.first!)" }

        let placeholder = JSQMessagesAvatarImageFactory.avatarImage(withUserInitials: initials, backgroundColor: UIColor.gray, textColor: UIColor.white, font: UIFont.systemFont(ofSize: 14), diameter: UInt(kJSQMessagesCollectionViewAvatarSizeDefault))

        MyImageCache.sharedCache.setObject(placeholder!, forKey: senderId as AnyObject)
    }
}

据我所知,JSQMessage中包含发送者的senderId,所以下面的函数已经指定了返回什么头像:

override func collectionView(_ collectionView: JSQMessagesCollectionView!,    avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource!    {
let message = messages[indexPath.row]
return avatars[message.senderId]
}

您需要为

添加头像
var avatars = [String: JSQMessagesAvatarImage]()

我假设你有那个用户的 userId,所以每次有人加入对话时都以相同的方式获取头像。让我们说 userToChatWith:

    createAvatar(userToChatWith.userKey, senderDisplayName: userToChatWithName,  user: userToChatWith, color: UIColor.lightGray)