Swift Firebase Group Messenger数据-资料图片和名称

时间:2019-01-12 16:35:30

标签: ios swift firebase

我有一个群组通讯工具,正在尝试使用户的个人资料图像和名称保持最新,以防他们决定更改其个人资料图像或更新其名称。问题是当我填充单元格时,数据变得不匹配。因此,所有内容,图像和名称均已加载,但这是错误的人员加载了。

呼叫和设置所有用户(客户,雇员和企业)

这是将用户设置为数据结构

func getCustomerData() {
    Database.database().reference().child("user_profiles").observe(.childAdded, with: { snapshot in
        self.customerData.append(CustomerData(snapshot: snapshot))
        print(snapshot)
        print("Printed Customer Data")
    })
}

func getEmployeeData() {
    Database.database().reference().child("employees").observe(.childAdded, with: { snapshot in
        self.employeeData.append(EmployeeData(snapshot: snapshot))
        print(snapshot)
        print("Printed Employee Data")
    })
}

func getBusinessData() {
    Database.database().reference().child("Businesses").observe(.childAdded, with: { snapshot in
        self.businessData.append(BusinessData(snapshot: snapshot))
        print(snapshot)
        print("Printed Business Data")
    })
}

用于客户,员工和业务的数据结构。所有3种相同的结构类型

import UIKit
import Firebase

class CustomerData: NSObject {

var customerName: String?
var customerPicture: String?
var customerUID: String?

init(snapshot: DataSnapshot) {
    if let dictionary = snapshot.value as? [String: AnyObject] {
        customerName = dictionary["name"] as? String
        customerUID = dictionary["uid"] as? String
        customerPicture = dictionary["profPicString"] as? String
    }
}
}

单元设置

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! ChatMessageCell

    cell.chatLogController = self

    let customer = customerData[indexPath.item]
    let employee = employeeData[indexPath.item]
    let business = businessData[indexPath.item]

    let message = messages[indexPath.row]

    cell.message = message
    cell.customer = customer
    cell.employee = employee
    cell.business = business

    setupChatMessageCell(cell,message,customer,employee,business)

    if let text = message.text {
        cell.textView.text = text
        cell.bubbleWidthAnchor?.constant = estimateSizeOfText(text).width + 32
        cell.textView.isHidden = false
    } else if message.imageUrl != nil {
        cell.bubbleWidthAnchor?.constant = 200
        cell.textView.isHidden = true
    }

    cell.playButton.isHidden = message.videoUrl == nil

    return cell
}


private func setupChatMessageCell(_ cell: ChatMessageCell, _ message: GroupMessage, _ customer: CustomerData, _ employee: EmployeeData, _ business: BusinessData) {

    if message.fromId == Auth.auth().currentUser?.uid {
        //outgoing messages
        cell.bubbleView.backgroundColor = ChatMessageCell.blueColor
        cell.textView.textColor = .white
        cell.bubbleLeftAnchor?.isActive = false
        cell.bubbleRightAnchor?.isActive = true
        cell.profileImageView.isHidden = true
        cell.nameLabel.textColor = .gray
        cell.nameRightAnchor?.isActive = true
        cell.nameLeftAnchor?.isActive = false
        cell.nameLabel.text = customer.customerName?.description
        //cell.nameLabel.text = message.customerName
    } else if message.fromId == business.businessUID?.description {
        //incoming messagese
        let customerImage = business.businessPicture?.description
        cell.profileImageView.loadImageUsingCacheWithUrlString(customerImage!)
        cell.profileImageView.isHidden = false
        cell.bubbleView.backgroundColor = UIColor(red: 240, green: 240, blue: 240)
        cell.textView.textColor = .black
        cell.bubbleLeftAnchor?.isActive = true
        cell.bubbleRightAnchor?.isActive = false
        cell.profileImageView.isHidden = false
        cell.nameRightAnchor?.isActive = false
        cell.nameLeftAnchor?.isActive = true
        cell.nameLabel.textColor = .black
        cell.nameLabel.text = business.businessName
    } else {
        let customerImage = employee.employeePicture?.description
        cell.profileImageView.loadImageUsingCacheWithUrlString(customerImage!)
        cell.profileImageView.isHidden = false
        cell.bubbleView.backgroundColor = UIColor(red: 240, green: 240, blue: 240)
        cell.textView.textColor = .black
        cell.bubbleLeftAnchor?.isActive = true
        cell.bubbleRightAnchor?.isActive = false
        cell.profileImageView.isHidden = false
        cell.nameRightAnchor?.isActive = false
        cell.nameLeftAnchor?.isActive = true
        cell.nameLabel.textColor = .black
        cell.nameLabel.text = employee.employeeName
    }

    if let imageUrl = message.imageUrl {
        cell.messageImageView.loadImageUsingCacheWithUrlString(imageUrl)
        cell.messageImageView.isHidden = false
        cell.bubbleView.backgroundColor = .clear
    } else {
        cell.messageImageView.isHidden = true
    }
}

Firebase组消息结构

enter image description here

我需要有关如何将消息“ fromId”与合适的用户匹配的帮助。我有3个不同的个人资料,客户,员工和企业。到目前为止,正在为企业和员工消息设置错误的数据。客户的数据是正确的,这是第一个“ if else”语句。

如何加载数据

enter image description here

因此您可以看到他显示的姓名和个人资料图片与消息中的姓名不同。

0 个答案:

没有答案